I have used the android:onClick attribute extensively in my XML layouts for my Android application.


    android:onClick="onButtonClick" />

Is it proper to create an Interface to enforce the implementation of those onClick methods in Activities that use that layout file?

public interface MyButtonInterface {
    public onButtonClick(View v);

2 Answers 2


Why do you want an interface? Is it to enforce at compile time that the activity class has the methods that are defined in the onClick attributes in the XML?

Personally I wouldn't bother.

  • Who enforces the interface has the right methods?
  • Who enforces the right interface is used on each activity?

Just accept that due to the flexible nature of the interaction between Android activities & layouts, it's not possible to enforce at compile time that the onClick function actually exists. As far as I know, anyway. Anyone prove me wrong?

  • The interface is really to help other developers who may use that UI know what needs to be implemented, but then again they would have to implement the correct interface for that layout. I guess it is just something to accept as is. Aug 3, 2012 at 21:22
  • If you are one of these "other developers" you should really know what's in the XML file before using it somewhere new. The point of the XML files is that theye are easier to scan (and implement layouts in) than Java code, as you could very well create a layout entirely in code. With a quick scan of the XML you can at least find every "android:onClick" and make sure the Activity you are using at least has those methods. Nov 9, 2012 at 17:33
  • If you find you're XML is too complicated to follow, maybe you want to flatten you hierarchy for performance (and maintainability), or maybe it could be defined behavior for your code that there is always an interface for a layout that defines required methods and maybe provides the IDs to the views. In which case, you must ALWAYS implement the interface of the layout you use. It might be argued that this approach breaks the DRY method, as this "interface" is simply repeating what is defined in the XML already. Consider adding a comment to the top of the XML that is a summary and lists onClick Nov 9, 2012 at 17:38
  • What's missed here is that it's a lot easier to audit that each activity claims to implement the interface, than it is to audit that each implements all of the methods individually. Obviously, the XML and interface do have to be kept in consistency with each other, but that is simpler than keeping a lot of activities consistent with the xml. In short, this is a creative idea for an unusual problem. That said, there still might be a risk of these apparently unused methods being stripped by a tool such as proguard. Apr 30, 2015 at 18:32
<?xml version="1.0" encoding="utf-8"?> 
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:text="Click me!"  
    android:onClick="myFancyMethod" />  
<!-- even more layout elements -->

The above code just shows the XML implementation. Now in the background Android does nothing else than the Java code calling your method on a click event.

Note that with the XML above, Android will look for the onClick method myFancyMethod() only in the currently active Activity's .java file. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.

protected void onCreate(final Bundle savedInstanceState) {

public void myFancyMethod(final View view) {
  • 1
    This does not answer the question, I know how the attribute onClick works. Aug 3, 2012 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.