There is a four ways to use OnClickListener
.
First way
To define OnClickListener
within the method call site.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.myButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
}
}
First reasons why to avoid this is because it clutters up onCreate
method. This becomes even more apparent when you want to observe click events from multiple views.
The next reason to avoid this is because it doesn't promote code reuse if several buttons should do the same.
Second way
The second way is almost the same as first except implementation to field is assigned in the class.
public class MainActivity extends AppCompatActivity {
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.myButton);
button.setOnClickListener(clickListener);
}
}
This way is pretty the same as first one, only advantage is that the method could be reused for several buttons.
Third way
This way is to declare an inner class to implement OnClickListener
. If it will be used multiple times is better to define the instance as a field.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.myButton);
button.setOnClickListener(new ButtonClick());
}
class ButtonClick implements View.OnClickListener {
@Override
public void onClick(View v) {
// do something
}
}
}
The advantage of this way is that it helps to organize the code. You can easily collapse this internal class and forget about it until you need to look at it.
The other good reason is that it could be turned in public class and reused in other app areas.
Fourth way
The fourth way involves Activity to implement OnClickListener
.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = findViewById(R.id.myButton);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// do something
}
}
The first disadvantage that this way creates a public method in the Activity and there you should pass this
activity when setOnClickListener
is called.
Second reason to avoid this way is that if another button is added you should determine which button was clicked. Then you should use switch()
or if()
statements. It isn't performed because it wastes a cycle or several for each button click.
Last disadvantage for this way is that difficult to organize a class. In example you have an Activity which implements multiple interfaces. Suddenly all of the methods from these interfaces are intertwined together, this becomes more evident after you’ve added methods to some of those interfaces. Also now you can't add an interface with method named onClick
.
There is some differences between these ways, but you should choose your way according your code and needs