0

I'm developing a sample app for android and my level is beginner.

For simplicity assume that I have three buttons namely A,B,C.

Instead of these buttons triggering different functions every time as they are clicked, all these buttons trigger the same method "onClick" which is defined in XML.

How can I get the name of the button that triggered onClick method, in what way should I use the view class ?

Thanks in advance for your time.

Pumpkin
  • 1,993
  • 3
  • 26
  • 32

2 Answers2

0

Another Way.

public void onClick(View v) {
    // Perform action on click
    String msg;
    int id = v.getId();

    Button inB = (Button) v.findViewById(id);

    msg = "Clicked Button ID From Object " + inB.getId() + " Button";
    Toast.makeText(v.getContext(), msg, Toast.LENGTH_LONG).show();
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
0

Take a look at this. Should do exactly what you want, with some suggestions for other implementations as well :)

Community
  • 1
  • 1
hrickards
  • 1,061
  • 2
  • 9
  • 20