0

I'm confused about different uses of setOnClickListener in an Activity button actions. I found a various solutions but I'm sure there is some best/worse approach to implement it and also some "because".

I would understand (as subject) which is the best approach and which (and why) are not.

  1. call a private function in activity (class) and set the listener and all casts in it:

    public class MainActivity extends AppCompatActivity {
       Private Button myButton;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          OnClickButton();
       }
       private void OnClickButton(){
          myButton = (Button)findViewById(R.id.Button1);
          myButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                // button actions
             }
    
          });
       }
    }
    
  2. set a setOnClickListener in activity and then call appropriate function: (In this case I don't understand also why the view is defined as final in onClick)

    public class MainActivity extends AppCompatActivity {
       Private Button myButton;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          myButton.setOnClickListener(onClickListener);
       }
       private OnClickListener onClickListener = new OnClickListener() {
          @Override
          public void onClick(final View v) {
             // button actions
          }
       }
    
  3. simply as a function with argument myView by linking the function in xml file:

ON MAINACTIVITY.XML ADD:

    android:onClick="onButtonClick"

ON MAINACTIVITYCLASS:

    public class MainActivity extends AppCompatActivity {
       Private Button myButton;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
       }
       public void onButtonClick(View v){
          // button actions
       }
    }

NOTE: I found (need comfirms) that 3° way is not a good practice because is not supported in framesets.

If you have some more strong good coded solution, please add it.

Please try to clear the good practice and bad-practice differences, and why something is more correct or instead is a bad solution.

Hoping this could be useful to other people, I wrote this post also because many post I read were very old. Thank you.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marco
  • 133
  • 2
  • 13
  • 2
    https://stackoverflow.com/questions/30082892/best-way-to-implement-view-onclicklistener-in-android – Levon Petrosyan Aug 25 '17 at 14:27
  • 1
    visit here already answerd. https://stackoverflow.com/questions/30082892/best-way-to-implement-view-onclicklistener-in-android – Sahdeep Singh Aug 25 '17 at 14:29
  • I read that post and tried to create: public class ActivityMain extends Activity implements View.OnClickListener .. ..but I had error: "Class "MainActivity" must either be declared abstract or implement abstract method 'onClick(view)' in 'OnClickListener' – Marco Aug 25 '17 at 14:57

2 Answers2

1

I recommend using Butterknife library for bindings and OnClick methods. In my opinion it is most clear way to define it.

Butterknife: http://jakewharton.github.io/butterknife/

Examples:

@OnClick(R.id.submit)
public void sayHi(Button button) {
    button.setText("Hello!");
}


@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
   if (door.hasPrizeBehind()) {
      Toast.makeText(this, "You win!", LENGTH_SHORT).show();
   } else {
      Toast.makeText(this, "Try again", LENGTH_SHORT).show();
   }
}
Jakub Anioła
  • 310
  • 3
  • 16
  • 1
    Tank you for suggestion, I'll see this solution, but I would prefer to understand the basics of Android-studio libraries, before implementing new ones. – Marco Aug 25 '17 at 14:37
0

I prefer setting method for onclick in xml layout and defining switch block to handle different events based on id. This method is more readable and has better performance than using bunch of anonymous inner classes.

public class MainActivity extends AppCompatActivity {
Private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
   {
      int id = v.getId();  
      switch(id)
         {
          case R.id.signinbutton: signin();break;
          case R.id.logoutbutton: logout();break;
         }
   }
}
KingKongCoder
  • 600
  • 4
  • 15