How to add Button widget in android using Java and Kotlin

 you can follow these steps:

Java

  • Open the XML layout file where you want to add the Button widget.
  • Add the following code where you want to add the Button widget:

//xml
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Text" />

  • In your Java file, use the findViewById() method to access the Button and set its click listener:

//java

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Add your code here for the button click event
    }
});


Kotlin

  • Open the XML layout file where you want to add the Button widget.
  • Add the following code where you want to add the Button widget:

//xml
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Text" />
  • In your Kotlin file, use the findViewById() method to access the Button and set its click listener:

///kotlin
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
    // Add your code here for the button click event
}

Note: Don't forget to import the necessary classes in your Kotlin file, such as Button and View.

கருத்துரையிடுக

Post a Comment (0)

புதியது பழையவை