Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

Example:

<Button
    android:id="@+id/exampleButton"
    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);
}
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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?

share|improve this answer
    
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. –  Austyn Mahoney Aug 3 '12 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. –  Dandre Allison Nov 9 '12 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 –  Dandre Allison Nov 9 '12 at 17:38
<?xml version="1.0" encoding="utf-8"?> 
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    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) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);
}

public void myFancyMethod(final View view) {
    System.out.println("clicked");
}
share|improve this answer
1  
This does not answer the question, I know how the attribute onClick works. –  Austyn Mahoney Aug 3 '12 at 21:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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