31

I have read this where found that how to access integer resources in java class but no docs for another resource.

Here is Resources at res/values/integers.xml

<resources>
     <integer name="input_field_padding" >5</integer>
</resources>

Tried to access the input_field_padding in EditText.

<EditText
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:padding="@integer/input_field_padding" />

But I got the following exception

java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10

Is it possible to access it in another resources file or am I missing something?

Rafiq
  • 2,000
  • 2
  • 21
  • 27

6 Answers6

34

Finally I am able to access integer resource in java code and integer value as dimen in xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="input_field_padding">20dip</dimen>
    <integer name="input_field_padding">20</integer>
</resources>

In xml file:-

<EditText
    android:id="@+id/user_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:padding="@dimen/input_field_padding" /> 

In java file:-

EditText mUsername = (EditText) this.findViewById(R.id.user_name);
//int padding = this.getResources().getInteger(R.integer.input_field_padding);
int padding = (int) this.getResources().getDimension(R.dimen.input_field_padding);
mUsername.setPadding(padding, padding, padding, padding);
Rafiq
  • 2,000
  • 2
  • 21
  • 27
  • 4
    This answer is mis-guiding. And depending on use case, wrong. Please see my answer below. – Ifrit Apr 14 '13 at 18:03
  • 2
    You've created the integer, which isn't illegal, but is non-standard, but then you never use it. You've only used the dimen that you created with the same name as the integer. This is bad practice at the least. – dm78 Dec 18 '13 at 22:28
  • @DavidMays Yes, the commented line isn't illegal and it worked for me. According to your suggestion I updated it. – Rafiq Dec 21 '13 at 09:19
  • 1
    You must use this in XML like : @integer/input_field_padding – Arslan Anwar Jan 12 '15 at 07:04
  • 1
    I am surprised why there are so many upvote on this, in fact, you use a dimen not integer. – zdd Mar 03 '15 at 13:43
  • This worked for me: int width = getResources().getInteger(R.integer.review_first_column_width); – Mike6679 Mar 16 '16 at 17:08
10

The accepted answer is completely mis-guiding. Unless there is a specific unique reason, you should never use an Integer resource for setting padding sizes. Not only in XML but also in code. This is why you experienced an UnsupportedOperationException. Integer resources are not scaled based on the DPI of screens. Which means you will not get consistently spaced padding for all devices. Dimen resources will automatically adjust their value for you. Your java code should look like this:

EditText mUsername = (EditText) this.findViewById(R.id.user_name);
int padding = (int) this.getResources().getDimension(R.dimen.input_field_padding);
mUsername.setPadding(padding, padding, padding, padding);

Which, btw, there's no need to set the padding of the EditText element in code if you already set it in the XML. Unless you wanted to change it to a different value at run time.

Read more here:
Density Independence
Working with XML Layouts

Ifrit
  • 6,791
  • 8
  • 50
  • 79
4

Also You Can Define It This Way :

<resources>
<item name="text_line_spacing" format="integer" type="dimen">10</item>
</resources>

Format = enclosing data type:

float boolean fraction integer ... and type stands for:

Type = resource type (referenced with R.XXXXX.name):

color dimen string style etc...

Access It Like This :

Resources res = getResources();
int i= res.getInteger(R.dimen.int_value);
Arash
  • 3,013
  • 10
  • 52
  • 74
2

if you are taking Integer resource then you have to take R.Integer.yourintvalue like this way

int value=(int) contextG.getResources()
            .getDimension(R.integer.myvalue);
Sydroid
  • 509
  • 1
  • 8
  • 16
-1

The correct way to access an integer field is using @integer/input_field_padding

In XML

 <EditText
        android:id="@+id/user_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:padding="@dimen/input_field_padding" /> 

In JAVA

R.integer.integer_name

Refer this http://developer.android.com/guide/topics/resources/more-resources.html#Integer .

100rabh
  • 6,156
  • 5
  • 27
  • 41
  • Agreed, this is the right way to do it. For reference, my `integer_name` value is in `values/integers.xml` like this: `42` – VinceFior Jul 25 '14 at 18:22
  • 2
    Using `R.integer.integer_name` directly is wrong! It just holds the `resource id` of the integer. you should use `getResources().getInteger(R.integer.SPLASH_SCREEN_DELAY)` as mentioned in Arash's answer ( http://stackoverflow.com/a/23267383/4718314 ) – Buddy Sep 28 '15 at 07:17
-1
 <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"

android:text="@integer/imageSelectLimit"

       android:textColor="@color/colorPrimary"
       android:textSize="@dimen/textSize"/>
Hemant Sharma
  • 259
  • 5
  • 14