Android : Multiple Button One Listener

Hi you may have ran into problem where you have multiple button in the layout and want want a single listener, so the code become more manageable. Here is how you can manage it.

This below code work for me after several attempt.

So here is what you need.
1.Layout with multiple buttons.
2.create a View.OnClickListener which you will need to attach with the button
[Note : I don't know why if the View.OnClickListener is initalised after, assigning the button listener it does not work. View.OnClickListener must be initalised befor assigning it to the button.]
3.Simple switch case which will check which button is clicked.

Below is the code which work for me.



public class EventHandlers extends AppCompatActivity {

    Button b1, b2, b3, b4;
    Context con; //need by toast
    View.OnClickListener myListner;  //list to be created

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //your layout
        setContentView(R.layout.activity_event_handlers);

        con = this;

        //Listner this will be called by the button listner
        //Listner must be created before setting 
        //the listner otherwise it will not work
        myListner = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                    case R.id.button2:
                        Toast.makeText(EventHandlers.this,
 "button 1 clicked", Toast.LENGTH_SHORT).show();
                        Log.d("event ", "1");
                        break;
                    case R.id.button3:
                        Toast.makeText(EventHandlers.this,
 "button 2 clicked", Toast.LENGTH_SHORT).show();
                        Log.d("event ", "2");
                        break;
                    case R.id.button4:
                        Toast.makeText(EventHandlers.this, 
"button 3 clicked", Toast.LENGTH_SHORT).show();
                        Log.d("event ", "3");
                        break;
                    case R.id.button5:
                        Toast.makeText(EventHandlers.this, 
"button 4 clicked", Toast.LENGTH_SHORT).show();
                        Log.d("event ", "4");
                        break;

                }
            }
        };


        //find the button
        b1 = (Button) findViewById(R.id.button2);
        b2 = (Button) findViewById(R.id.button3);
        b3 = (Button) findViewById(R.id.button4);
        b4 = (Button) findViewById(R.id.button5);

        //set the listner
        b1.setOnClickListener(myListner);
        b2.setOnClickListener(myListner);
        b3.setOnClickListener(myListner);
        b4.setOnClickListener(myListner);


    }


}

Here is is layout xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_event_handlers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="skd.app.passwordmanager.EventHandlers">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="104dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="21dp"
        android:id="@+id/button2" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_marginTop="28dp"
        android:id="@+id/button3" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="21dp"
        android:layout_marginEnd="21dp"
        android:id="@+id/button4" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2"
        android:layout_marginTop="17dp"
        android:id="@+id/button5" />
</RelativeLayout>

Comments