I have simple layout, but I can only set string tag. How to set integer tag?
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:src="@drawable/image" />
UPDATE
I found out how to set Integer tags in xml layout. We need to specify an integer variable in any xml resource file. That should look like that:
res/values/value.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<integer name="int1">15</integer>
<integer name="int2">1</integer>
</resources>
And now we are free to use "@integer/int1" or "@integer/int2" as tags for our xml widgets, for example:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="@integer/int2"
android:src="@drawable/image" />
However, in my case I preferred to set tag programmatically :)
I used the following to setup the tag in xml and handle it later in code:
In xml you can only set String. But in code you can use
View.setTag(int value);
because it takes Object. To read a value you need to cast it to Integerint value = (Integer)view.getTag();
From the author's edit I attempted to use
@integer/int2
to set the tag as an integer, but it still seems thatgetTag()
is returning the tag as aString
(at least in Jellybean).Integer.parseInt(String)
can convert aString
to anInteger
and@integer/int2
can validate that your tag is a properInteger
. So if you want to put anInteger
in a tag through XML that is probably the best route. Downside, since it usesparseInt
it likely takes a little more time than having it stored as a int the whole time.Supply a tag for this view containing a String, to be retrieved later with
View.getTag()
or searched for withView.findViewWithTag()
.Must be a string value, using
'\\;'
to escape characters such as'\\n'
or'\\uxxxx'
for a unicode character.For more information go to android:tag