Is it possible to make a secondary class to hold the OnClick Listener? Meaning not being created in the Activity class?
I just find that putting OnClick listeners in the main activity class is just messy and I would rather have them in separate classes. Thanks
Android three ways to set click listener In Android we can set click listener by three ways Setting individual click listener for each element. Implementing OnClickListener in activity level. Creating a separate function for handling button click and add this function in xml
I just find that putting OnClick listeners in the main activity class is just messy and I would rather have them in separate classes. Thanks Show activity on this post. Sure, that's possible. Just create a class that implements View.OnClickListener and set that as listener to the View. For example:
setOnClickListener is a method in Android basically used with buttons, image buttons etc. You can initiate this method easily like, public void setOnClickListener (View.OnClickListner) While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.
Yes you can. However, making the listener an inner class has one advantage - it can access the fields and variables of your activity class directly. If you make it a separate class, and your listener actually need to access 5 views, your listener constructor might look like this:
Sure, that's possible. Just create a class that implements View.OnClickListener
and set that as listener to the View
. For example:
public class ExternalOnClickListener implements View.OnClickListener {
public ExternalOnClickListener(...) {
// keep references for your onClick logic
}
@Override public void onClick(View v) {
// TODO: add code here
}
}
And then set an instance of above class as listener:
view.setOnClickListener(new ExternalOnClickListener(...));
The parameterized constructor is optional, but it's very likely you'll need to pass something through to actually make your onClick(...)
logic work on.
Implementing a class anonymously is generally easier to work with though. Just a thought.
Instead of putting the onCLicklistener
in a separate class, why dont you try to define onClickListener
outside onCreate()
??
For e.g: like this
onCreate()
yourViewName.setOnClicklistener(listener):
Outside onCreate()
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
Yes you can. However, making the listener an inner class has one advantage - it can access the fields and variables of your activity class directly. If you make it a separate class, and your listener actually need to access 5 views, your listener constructor might look like this:
MyListener listener = new MyListener(context, button, textView1, textView2, ratingBar, imageView);
Which is kinda bulky too. If your listener is simple, go ahead and make it a separate class. Otherwise, its up to you for readability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With