Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set integer tag to a widget in xml layout file?

Tags:

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 :)

like image 686
Eugene Chumak Avatar asked Feb 14 '12 10:02

Eugene Chumak


People also ask

Can integers be stored as resources in XML?

An integer defined in XML. Note: An integer is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer resources with other simple resources in the one XML file, under one <resources> element. The filename is arbitrary.

What does the tag in an XML layout file do?

The <include> tag lets you to divide your layout into multiple files: it helps dealing with complex or overlong user interface. Then you need to write include1. xml and include2.

Which method is used to access any widgets of XML file?

Solution: When you define your Android widgets in XML and then need to find a widget by its id , the method you need is named findViewById ., and it's in the View object of your layout.

What an XML layout file contains?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.


2 Answers

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 Integer int value = (Integer)view.getTag();

like image 137
zoki Avatar answered Oct 03 '22 21:10

zoki


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

like image 44
Dandre Allison Avatar answered Oct 03 '22 22:10

Dandre Allison