How to Apply One Listener to Multiple Buttons in Android?
Last Updated :
06 Jun, 2021
In this article we are going to write a shortcode for applying click events over different buttons, rather than writing different methods for different buttons, we are going to build only a single method that is onClick() for all buttons present and by using the concept of switch case we can perform different activities over different buttons. Now, without wasting further time let’s look at the implementation.
What we are going to build in this article?
In this article, we will develop a sample application that will contain three buttons and by clicking those three buttons we can perform different actions by using only a single onClick() method. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Now it’s time to design the layout of the application. So for that go to the app >res > layout > activity_main.xml and paste the below-written code in the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:id = "@+id/root_layout"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_marginTop = "220px"
android:text = "Button 1" />
< Button
android:id = "@+id/button2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@id/button"
android:layout_centerHorizontal = "true"
android:text = "Button 2" />
< Button
android:id = "@+id/button3"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@id/button2"
android:layout_centerHorizontal = "true"
android:text = "Button 3" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity.java file
Go to the app > java > package name > MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2 ,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1= findViewById(R.id.button);
btn2= findViewById(R.id.button2);
btn3= findViewById(R.id.button3);
btn1.setOnClickListener( this );
btn2.setOnClickListener( this );
btn3.setOnClickListener( this );
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Toast.makeText( this , "button1 pressed" , Toast.LENGTH_SHORT).show();
break ;
case R.id.button2:
Toast.makeText( this , "button2 pressed" , Toast.LENGTH_SHORT).show();
break ;
case R.id.button3:
Toast.makeText( this , "button3 pressed" , Toast.LENGTH_SHORT).show();
break ;
}
}
}
|
That’s all, now the application is ready to install on the device. Here is what the output of the application looks like.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...