-3

can anyone explain with an example as I have read some of the answers on stack overflow but I am unable to understand its working properly

1 Answers1

0

Tag can be any information you want to use along with your view.

Tags are added by setTag

button1.setTag("Hello");
button2.setTag("World");

And received by getTag

listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.print(v.getTag());
    }
};

button1.setOnClickListener(listener);
button2.setOnClickListener(listener);

Since the listener is same in both button, but information tagged is different, so when button1 is clicked it will print "Hello" and button2 click will print "World".

You may use any objects instead of strings

Amit
  • 2,389
  • 22
  • 29