0

I am new to android i want to invoke method on button click using XML without using clickListener can any one provide me source code for this functionality.

Asad Farooq
  • 191
  • 1
  • 13

2 Answers2

4

You just need to define name of method in onClick attribute.

<Button android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:onClick="methonName" />

Now you declared same name method in java class.

public void methonName(View v) {
    // Do your stuff..
}
Ehsan Ullah
  • 175
  • 10
2

you can uses android:onClick="someMethod()" inside xml and declare the same function with the same name inside the java or kotlin code. and whenever click action happens on the button the function will be called and the method code inside will execute