Android button onclicklistener switch case

Here are four ways to implement OnClickListener in Android without the use of a third party library. This is the way I learned to handle click events on views back in For several years I was fairly content with this method, but it is now something I almost never do.

Cubeactive’s official blog

One reason why I avoid this is because it clutters up your onCreate method. This becomes even more apparent when you want to observe click events from multiple views. Of course retrolambda or simply using Kotlin would make it possible to take this from 5 lines down to a single line. Fear not, I have two more reasons to avoid this. The final, and probably best reason to avoid this is this is really difficult to unit test.


  1. Android: Grouping onClickListeners together by implementation in an activity?
  2. collins cobuild advanced dictionary of english free download for samsung mobile.
  3. Binding onClick for Multiple Button · Issue # · JakeWharton/butterknife · GitHub.
  4. Your Answer!
  5. download instagram app for blackberry 9790!
  6. 10 apps all new android users.
  7. OnClickListener for Multiple Buttons - Coding in Flow!

On the other hand if you provided more seams or areas to inject functionality into this class you could change the implementation of this click event to be a mock or fake and can test this without needing a heavy framework. As already said I did things this way for many years, just be aware of some of the drawbacks and limitations that come with doing things this way.

The second option is kind of like the first one except we assign the implementation to field in the class. This has been one of my goto ways to implement OnClickListener as of recently.

Popular classes and methods

One reason why I like this option is it is incredibly easy to refactor option one into option two. All you have to do is take your implementation and move it out of setOnClickListener , assign it to a field, and then update your call to setOnClickListener to reference that field.

Introduction to OnClickListeners

Another reason why I like this option is it allows you to reuse the implementation. So when one of our customers demands that we add a second button that does the exact same thing as our OG awesome button, all we have to do is call setOnClickListener and pass in the field.

OnClickListener for Multiple Buttons - Android Studio Tutorial

The last reason I wanted to bring up is this creates a seam that will allow us to test this class easier. Oh, another positive to this route is it can help you organize your code a bit better as well. This option takes option two one step further by declaring a class to implement OnClickListener.

Android Tutorial: Using Switch Case Statement - BigKnol

Context menu example on long press, gridview Supporting multiple phones and screensizes in your android applications. Android , eclipse , java. Thanks Stealthcopter. BlueBot September 30, at Abhi December 26, at Its not working for me.

Android Button Click example

My app is failing to load. Eugene van der Merwe January 16, at Nice way of keeping code clean, thanks a lot. Colin May 24, at V nice. Makes things more readable and cuts down on total lines. Steve Parker July 26, at Leave a Comment Cancel comment Your email address will not be published.


  1. activate windows xp professional by phone crack.
  2. Android Tutorial: How to implement an OnClickListener | Cubeactive blog?
  3. How to create a button in Android and capture the OnClick event;
  4. Blog Archive.
  5. record phone calls iphone 5 uk.
  6. Popular methods of View?
  7. Public constructors;

Context menu example on long press, gridview.