Accessing integer resources in xml

Rafiq picture Rafiq · Apr 19, 2012 · Viewed 30.6k times · Source

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?

Answer

Rafiq picture Rafiq · Jul 10, 2012

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