Having a solid experience in non-Java and non-Android area, I\'m learning Android.
I have a lot of confusion with different areas, one of them is how to handle butt
There are also options available in the form of various libraries that can make this process very familiar to people that have used other MVVM frameworks.
https://developer.android.com/topic/libraries/data-binding/
Shows an example of an official library, that allows you to bind buttons like this:
<Button
android:text="Start second activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> presenter.showList()}"
/>
#1 I use the last one frequently when having buttons on the layout which are not generated (but static obviously).
If you use it in practice and in a business application, pay extra attention here, because when you use source obfuscater like ProGuard, you'll need to mark these methods in your activity as to not be obfuscated.
For archiving some kind of compile-time-security with this approach, have a look at Android Lint (example).
#2 Pros and cons for all methods are almost the same and the lesson should be:
Use what ever is most appropriate or feels most intuitive to you.
If you have to assign the same OnClickListener
to multiple button instances, save it in the class-scope (#1). If you need a simple listener for a Button, make an anonymous implementation:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Take action.
}
});
I tend to not implement the OnClickListener
in the activity, this gets a little confusing from time to time (especially when you implement multiple other event-handlers and nobody knows what this
is all doing).
Most used way is, anonymous declaration
Button send = (Button) findViewById(R.id.buttonSend);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handle click
}
});
Also you can create View.OnClickListener object and set it to button later, but you still need to override onClick method for example
View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
// handle click
}
}
Button send = (Button) findViewById(R.id.buttonSend);
send.setOnClickListener(listener);
When your activity implements OnClickListener interface you must override onClick(View v) method on activity level. Then you can assing this activity as listener to button, because it already implements interface and overrides the onClick() method
public class MyActivity extends Activity implements View.OnClickListener{
@Override
public void onClick(View v) {
// handle click
}
@Override
public void onCreate(Bundle b) {
Button send = (Button) findViewById(R.id.buttonSend);
send.setOnClickListener(this);
}
}
(imho) 4-th approach used when multiple buttons have same handler, and you can declare one method in activity class and assign this method to multiple buttons in xml layout, also you can create one method for one button, but in this case I prefer to declare handlers inside activity class.
My sample, Tested in Android studio 2.1
Define button in xml layout
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java pulsation detect
Button clickButton = (Button) findViewById(R.id.btn1);
if (clickButton != null) {
clickButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
/***Do what you want with the click here***/
}
});
}
I prefer option 4, but it makes intuitive sense to me because I do far too much work in Grails, Groovy, and JavaFX. "Magic" connections between the view and the controller are common in all. It is important to name the method well:
In the view,add the onClick method to the button or other widget:
android:clickable="true"
android:onClick="onButtonClickCancel"
Then in the class, handle the method:
public void onButtonClickCancel(View view) {
Toast.makeText(this, "Cancel pressed", Toast.LENGTH_LONG).show();
}
Again, name the method clearly, something you should do anyway, and the maintenance becomes second-nature.
One big advantage is that you can write unit tests now for the method. Option 1 can do this, but 2 and 3 are more difficult.
To make things easier asp Question 2 stated, you can make use of lambda method like this to save variable memory and to avoid navigating up and down in your view class
//method 1
findViewById(R.id.buttonSend).setOnClickListener(v -> {
// handle click
});
but if you wish to apply click event to your button at once in a method.
you can make use of Question 3 by @D. Tran answer. But do not forget to implement your view class with View.OnClickListener
.
In other to use Question #3 properly