Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - OnClick Listener in a separate class

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

like image 581
Pztar Avatar asked Nov 21 '12 03:11

Pztar


People also ask

How do I set a click listener in Android?

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

Is it possible to put onclick listeners in separate classes?

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:

What is setonclicklistener in Android?

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.

Can I use a listener inside an activity class?

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:


3 Answers

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.

like image 154
MH. Avatar answered Oct 19 '22 04:10

MH.


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

        }
    };
like image 11
Renjith Avatar answered Oct 19 '22 04:10

Renjith


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.

like image 7
Lawrence Choy Avatar answered Oct 19 '22 04:10

Lawrence Choy