Deploy ViewClickListner in your activity class. Override the 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 used a toast message.
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Toast.makeText(MainActivity.this,"button1", 1000).show();
Using the switch case, you can check which button is pressed.
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 is the full 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) {
source share