[ACCEPTED]-How to set integer tag to a widget in xml layout file?-tags

Accepted answer
Score: 17

In xml you can only set String. But in code 3 you can use View.setTag(int value); because it takes Object. To 2 read a value you need to cast it to Integer 1 int value = (Integer)view.getTag();

Score: 4

From the author's edit I attempted to use 9 @integer/int2 to set the tag as an integer, but it still 8 seems that getTag() is returning the tag as a String (at 7 least in Jellybean). Integer.parseInt(String) can convert a String to 6 an Integer and @integer/int2 can validate that your tag is a 5 proper Integer. So if you want to put an Integer in a 4 tag through XML that is probably the best 3 route. Downside, since it uses parseInt it likely 2 takes a little more time than having it 1 stored as a int the whole time.

Score: 3

I used the following to setup the tag in 1 xml and handle it later in code:

<?xml version="1.0" encoding="UTF-8"?>
<string name="item_tag">1</string>

<!-- TextView with Tag -->

// retrieve the tag
int itemTag = Integer.valueOf((String) textView.getTag()); // itemTag == 1
Score: 0

Supply a tag for this view containing a 5 String, to be retrieved later with View.getTag() or searched 4 for with View.findViewWithTag().

Must be a string value, using 3 '\\;' to escape characters such as '\\n' or '\\uxxxx' for a 2 unicode character.

For more information 1 go to android:tag

More Related questions