Android API guides are a bit unclear to me about android: onClick XML attribute

In the Android API Guides for checkboxes , they state that you can set the attribute in XML format ( android:onClick ) to run the assigned method. The same thing is said in the button manual , but the button manual also has this bit:

You can also declare a click event handler pragmatically, rather than in an XML layout. This may be required if you create an instance of Button at runtime or you need to declare click behavior in a subclass of Fragment .

The following question arises: can you or can not use the android:onClick attribute if the button or checkbox is in the Fragment? If so, I would suggest that the method that will be called will be declared in the Fragment, right?

+4
source share
2 answers

You can use the onClick attribute in fragments. However, since a Fragment is part of the Activity , the assigned method will be run in the Activity class, not in the fragment.

+3
source

The target method for android:onClick must be defined in the activity in which the fragments are placed.

+3
source

All Articles