2

I have just started to learn Android Application Development from thenewboston.com tutorials.

I had a confusion with setting the onClickListener event handler. When setting it for a button in the main activity, they used the Button class.

    redButton.setOnClickListener(
      new Button.onClickListener(){
              public void onClick(View v){
                 // Do Something 
             }
        }
    );

But when setting it for a fragment they used the view class.

    redButton.setOnClickListener(
          new View.onClickListener(){
                  public void onClick(View v){
                     // Do Something 
                 }
            }
        );

What is the difference between the two ? And when to use them ? Please Help !!

Ayusch
  • 417
  • 7
  • 17
  • activity also we can use View – Android Oct 24 '15 at 07:05
  • Button class is derived from View class.So you can set click listener from either of the type and compiler won't complain you.Better to use the specific listener type always.In this case use "Button.onClickListener". – Deshan Oct 24 '15 at 07:08
  • but when i used the Button class the app crashed. – Ayusch Oct 24 '15 at 07:13

3 Answers3

0

In Button also, you can use View also as you are using in fragment. Actually both works identically.It is just the way you want to go for better solution than other.

For more details you could refer:

setOnclickListener vs OnClickListener vs View.OnClickListener

Difference between specifying the view.onclicklistener and just having onclicklistener

Community
  • 1
  • 1
prat
  • 597
  • 8
  • 17
0

Button is derived class of View, so onClickListener of Button is overridden from onClickListener for View.

And there is no difference between two implementations.

In the documentation for button example uses view.onClickListener

 final Button button = (Button) findViewById(R.id.button_id);
 button.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
         // Perform action on click
     }
 });

so that seems the recomanded way.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • Thanks!! It was a very simple explanation. I have read your answers on some other posts as well relating to android. As I am just starting to develop for android I had some questions. I wonder if you could just give me your email ID so I may contact you? – Ayusch Oct 24 '15 at 07:31
  • we can have chat on SO. – Rahul Tiwari Oct 24 '15 at 10:28
  • I don't know how it works. Would be glad if you would start. Thank You – Ayusch Oct 24 '15 at 12:19
0

Button.onClickListener() could only be used for Button. But View.onClickListener() could be used for any view. So what is the right way of doing it?

Well, what you have currently done is called 'setting an anonymous listener`. That's bad. More info here: https://softwareengineering.stackexchange.com/a/110113

The best way to do is let your Activity or Fragment implement the View.onClickListener() and the override the onClick() method.

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98