Skip to content

Android: Grouping onClickListeners together by implementation in an activity

by mat on April 16th, 2010

Here is a quick tip for better organisation of your program by grouping all onClickListeners into a simple switch statement.

The problem
If you have lots of buttons or view that you are linking to OnClickListener events you can quickly end up with some cumbersome code. This can be changed very simply by implementing a method in your activity and switching between View ID’s.

Below is an example the typical method for creating an OnClickListener for a View.

final ImageButton Ibutton = (ImageButton) findViewById(;
Ibutton.setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
		Toast.makeText(testActivity.this, "Button 1 pressed ", Toast.LENGTH_SHORT).show();

final ImageButton Ibutton2 = (ImageButton) findViewById(;
Ibutton2.setOnClickListener(new OnClickListener() {
	 public void onClick(View v) {
		Toast.makeText(testActivity.this, "Button 2 pressed", Toast.LENGTH_SHORT).show();

The Solution
We can get our activity to handle the OnClick events itself, we do this we implementing OnClickListener:

public class testActivity extends Activity implements OnClickListener {

If your write this in eclipse you can hover over the error this produces and click Add unimplemented methods to automatically create a stub for the next part.

eclipse auto suggesting to implement the onclicklistener method

eclipse auto suggesting to implement the onclicklistener method

We now create our buttons similar to before, but for the setOnClickListener we pass this as an argument, so that our implemented method is called.

ImageButton Ibutton = (ImageButton) findViewById(;
ImageButton Ibutton2 = (ImageButton) findViewById(;

We now create the implemented method for our activity (onClick) and we use a switch statement to find which View fired the onclick event and preform an action accordingly.

public void onClick(View v) {
		// action to preform on button 1
		Toast.makeText(testActivity.this, "Button 1 pressed ", Toast.LENGTH_SHORT).show();
		// action to preform on button 1
		Toast.makeText(testActivity.this, "Button 2 pressed ", Toast.LENGTH_SHORT).show();

And it’s as simple as that!
Feel free to post comments or questions

  1. superMeat permalink

    Thanks Stealthcopter. This was a great halp ^_^

  2. BlueBot permalink

    thx that really helps apps with a large amount of buttons

  3. Abhi permalink

    Its not working for me. My app is failing to load.

  4. @Abhi Check your logcat for errors, usually explains what’s going wrong.

  5. Nice way of keeping code clean, thanks a lot.

  6. Colin permalink

    V nice. Makes things more readable and cuts down on total lines

  7. Thanks, that’s really helpful.

  8. ikki_fenix permalink

    One question, If you want to call two buttons in two differents layouts. Is the same code? Or I need to add something else.
    Thanks for your replay!

  9. sameer permalink

    thans for ur help….was good to see my small code started working thanx bro

  10. she just wasn’t happy anymore and didn’t want this kind of life. Here’s where it gets messy for the next two mothns, we talked daily for hours, hung out, and messed around. She said we should have some space for a month because she still wanted me around because she thinks i am a great person. Problem is we kept breaking the space rule. In the mean time, she had a crush on a guy at work. I know that YOU DO NOT DATE PEOPLE YOU WORK WITH!!!!!! She told me that she doesn’t want to get into a relationship for a while, definitely does not want sex with anyone, but is willing to take a risk to date someone so she never wonders why later on. she has hung out with this guy, he has kissed her. She told me she was going to have a talk with him about not getting into a relationship, but I have no idea whats been going on since. Because I know she sees him and talks to him daily at work, and I am sure they have hung out since that one night. We both knew what we were still doing was wrong, but it was hard because at the same time it felt so right. The next thing that happened is the psychical part stopped. I already made the mistake of trying to talk this out, getting sad and crying in front of her, which made her upset also. The last couple times we saw each other, she wouldn’t let me touch her at all, all I could do was hug her. So next we came down to the point where we were going to give the space. it has been two weeks, and two weeks to go before it is ok to contact her. Right now I am already planning on not contacting her on this exact date because I know not to seem desperate and needy. And simply because she knows the exact date also. I am going to wait for the rest of that week (2 weeks from now) and see if she contacts me first before I make the call. In the mean time, I have texted her twice, but now continuing to stay strong. I wrote down her number and deleted it from my phone to take away the temptation. I know if she is dating its not the end of the world, it’s not like she’s going to marry the next person. In my eyes, I know I can correct things and we can be better together than before, I just need the chance. She says we will never get back together because she doesn’t see herself marrying me. Yet on the other hand, one time the hypothetically if we did ..wouldn’t be for a long time yadda yadda yadda .. came up, So who knows. Seriously ladies, is it possible for a man to win a woman back? I want to know the secrets, I have gotten some info from books already, but I feel I need a woman’s point of view you know??? I know this is long, but I am trying to provide as much info as I can to make you understand the situation. I appreciate anyone who has read this and it would be nice to see some advice. I still love her so much and I truly believe we can be happy together, thanks for your time and patience, I believe my email is attached so if you have a question or need to know more, feel free to contact me, thanks again!!!!!!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS