One Onclickhandler for Multiple Buttons

One OnClickHandler for multiple Buttons

You can also set it in your layout xml using the android:onclick attribute.

android:onClick="onClick"

Then in your activity class add the onClick method.

public void onClick(View v) {
...

Here's the documentation.

onClick function for multiple buttons

The click listener receives a view as parameter, you can use that to identify the button by it's id,

val clickListener = View.OnClickListener { button ->
val feeling = when (button.id) {
R.id.button_1 -> /* get feeling */
R.id.button_2 -> /* ... */
...
else -> return
// use the feeling to do whatever you need
}

You would then set this click listener to all your buttons.

Edit:
to set the click listener, you have different options. You can use findViewById for each of them, using a binding object and then bind the click listener, it depends on your setup.

For instance

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.findViewById<Button>(R.id.button_1).setOnClickListener(clickListener)
view.findViewById<Button>(R.id.button_2).setOnClickListener(clickListener)
}

Multiple Buttons' OnClickListener() android

You Just Simply have to Follow these steps for making it easy...

You don't have to write new onClickListener for Every Button... Just Implement View.OnClickLister to your Activity/Fragment.. it will implement new Method called onClick() for handling onClick Events for Button,TextView` etc.

  1. Implement OnClickListener() in your Activity/Fragment
public class MainActivity extends Activity implements View.OnClickListener {

}

  1. Implement onClick() method in your Activity/Fragment
public class MainActivity extends Activity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onClick(View v) {
// default method for handling onClick Events..
}
}

  1. Implement OnClickListener() For Buttons
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.your_layout);

Button one = (Button) findViewById(R.id.oneButton);
one.setOnClickListener(this); // calling onClick() method
Button two = (Button) findViewById(R.id.twoButton);
two.setOnClickListener(this);
Button three = (Button) findViewById(R.id.threeButton);
three.setOnClickListener(this);
}

  1. Find Buttons By Id and Implement Your Code..
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.oneButton:
// do your code
break;
case R.id.twoButton:
// do your code
break;
case R.id.threeButton:
// do your code
break;
default:
break;
}
}

Please refer to this link for more information :

https://androidacademic.blogspot.com/2016/12/multiple-buttons-onclicklistener-android.html (updated)

This will make easier to handle many buttons click events and makes it looks simple to manage it...

How to perform onclick action on multiple buttons created dynamically in Android Studio

just add onCLickListener in your activity

View.OnClickListener onClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
// put condition as per id of view
}
};

// here while creating dynamically add click listner

Button button=new Button(ButtonActivity.this);
button.setText("Button"+i);
button.setId(1000+i);
button.setOnCLickListener(onClickListener);

I hope i gave you solution.

Android one OnClick method for multiple buttons?

If you want to reduce the coding lines then use View's OnClick() with switch statement and if you want to handle separately all click (for easily understanding and maintaining code) then use separate all button's onClick().

Update:

If you have declared Buttons in your Activity layout xml file, than write attribute android:onClick="" with same method name for all buttons and implement that method in your activity. Now you have one method for all buttons and in that method differentiate buttons with id.

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 1" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 2" />
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 3" />
</LinearLayout>

Now in your Activity implement buttonOnClick like,

public void buttonOnClick(View view)
{
switch(view.getId())
{
case R.id.button1:
// Code for button 1 click
break;

case R.id.button2:
// Code for button 2 click
break;

case R.id.button3:
// Code for button 3 click
break;
}
}

Or you can apply same switch case for dynamically added buttons in your activity,
like instead of buttonOnClick you have to use implemented View's OnClickListerner's onClick.



Related Topics



Leave a reply



Submit