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
Asked
Active
Viewed 1,087 times
-3
-
Have you read this: https://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – Sweeper Sep 24 '17 at 13:51
-
Possible duplicate of [What is the main purpose of setTag() getTag() methods of View?](https://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view) – ישו אוהב אותך Sep 24 '17 at 13:51
-
yes I have read this – Apoorva Agarwal Oct 08 '17 at 13:09
1 Answers
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