From that I\'ve read you can assign a onClick
handler to a button in two ways.
Using the android:onClick
XML attribute where you just use t
Be careful, although android:onClick
XML seems to be a convenient way to handle click, the setOnClickListener
implementation do something additional than adding the onClickListener
. Indeed, it put the view property clickable
to true.
While it's might not be a problem on most Android implementations, according to the phone constructor, button is always default to clickable = true but other constructors on some phone model might have a default clickable = false on non Button views.
So setting the XML is not enough, you have to think all the time to add android:clickable="true"
on non button, and if you have a device where the default is clickable = true and you forget even once to put this XML attribute, you won't notice the problem at runtime but will get the feedback on the market when it will be in the hands of your customers !
In addition, we can never be sure about how proguard will obfuscate and rename XML attributes and class method, so not 100% safe that they will never have a bug one day.
So if you never want to have trouble and never think about it, it's better to use setOnClickListener
or libraries like ButterKnife with annotation @OnClick(R.id.button)
Add Button in xml and give onclick attribute name that is the name of Method.
<!--xml --!>
<Button
android:id="@+id/btn_register"
android:layout_margin="1dp"
android:onClick="addNumber"
android:text="Add"
/>
Button btnAdd = (Button) findViewById(R.id.mybutton); btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNumber(v);
}
});
Private void addNumber(View v){
//Logic implement
switch (v.getId()) {
case R.id.btnAdd :
break;
default:
break;
}}
Another way to set your on click listeners would be to use XML. Just add android:onClick attribute to your tag.
It is a good practice to use the xml attribute “onClick” over an anonymous Java class whenever possible.
First of all, lets have a look at the difference in code:
XML Attribute / onClick attribute
XML portion
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:onClick="showToast"/>
Java portion
public void showToast(View v) {
//Add some logic
}
Anonymous Java Class / setOnClickListener
XML Portion
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java portion
findViewById(R.id.button1).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//Add some logic
}
});
Here are the benefits of using the XML attribute over an anonymous Java class:
Of course, it is not always possible to use the Xml attribute, here are the reasons why we wouldn’t chose it:
android:onClick
is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.
Check if you forgot to put the method public!
To make your life easier and avoid the Anonymous Class in setOnClicklistener (), implement a View.OnClicklistener Interface as below:
public class YourClass extends CommonActivity implements View.OnClickListener, ...
this avoids:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourMethod(v);
}
});
and goes directly to:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.your_view:
yourMethod();
break;
}
}