Android OnClickListener - identify a button
You will learn the way to do it, in an easy way, is:
public class Mtest extends Activity {
Button b1;
Button b2;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
}
};
}
Or, if you are working with just one clicklistener, you can do:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
Though, I don't recommend doing it that way since you will have to add an if
for each button you use. That's hard to maintain.
How to tell which button was clicked in onClick()
Implement View's OnClickListner in your activity class. Override on click method.
Button b1= (Button) findViewById(R.id.button1);
//find your button id defined in your xml.
b1.setOnClickListener(this);
// You have button OnClickListener implemented in your activity class.
//this refers to your activity context.
I have used a toast message.
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Toast.makeText(MainActivity.this,"button1", 1000).show();
//display a toast using activity context ,text and duration
Using switch case you can check which button is clicked.
In your onClick method.
switch(v.getId()) //get the id of the view clicked. (in this case button)
{
case R.id.button1 : // if its button1
//do something
break;
}
Here's the complete code.
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button) findViewById(R.id.button1);
Button b2= (Button) findViewById(R.id.button2);
Button b3= (Button) findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1 :
Toast.makeText(MainActivity.this,"button1", 1000).show();
break;
case R.id.button2 :
Toast.makeText(MainActivity.this,"button2", 1000).show();
break;
case R.id.button3 :
Toast.makeText(MainActivity.this,"button3", 1000).show();
break;
}
}
}
Knowing which button was clicked in Android
I hope you can use tag option. You can set the tag for a view by view.setTag(1) and then on click event you can get back the tag set using view.getTag().
Detect which button was pressed
If i correctly understand you can do something like this:
final View.OnClickListener soundButtonOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v instanceof Button) {
Button button = (Button) v;
String text = button.getText().toString();
if(text == "1") {
//...
} else if(text == "2") {
}
//OR
switch (text) {
case "1": {
break;
}
case "2": {
break;
}
//...
}
}
}
};
But in my opinion better use tag instead of text:
//set tag for your button
button.setTag(number);
//use this tag
Integer number = (Integer) v.getTag();
if(number != null) {
//...
}
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.
- Implement
OnClickListener()
in yourActivity
/Fragment
public class MainActivity extends Activity implements View.OnClickListener {
}
- 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..
}
}
- 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);
}
- 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...
Android How to Identify Listview Button clicked event from outside adapter?
If your Activity
is an ListActivity
you can use onItemClick
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO
}
Related Topics
How to Get the Width and Height of an Android.Widget.Imageview
Android Spinner: Get the Selected Item Change Event
Android: How to Bind Spinner to Custom Object List
How to Include *.So Library in Android Studio
Check Android Application Is in Foreground or Not
Open Application After Clicking on Notification
Finished with Non Zero Exit Value
How to Get Onclick Event on Webview in Android
How to Customize Listview Using Baseadapter
CSS Media Query - Soft-Keyboard Breaks CSS Orientation Rules - Alternative Solution
Updating Google Play Services in Emulator
Generate Sha-1 for Flutter/React-Native/Android-Native App
Android Update Activity UI from Service
How to Programmatically Move, Copy and Delete Files and Directories on Sd
Android: Unable to Add Window. Permission Denied for This Window Type