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?
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.
XML files that contain simple values, such as strings, integers, and colors. Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources.
You are changing values in file which is generated by Android Studio, so it is changed every time you build your project. All files inside build folders are generated and there is no need in changing them. Instead you should put your values in values. xml under app->src->main->res->values .
xml is not created in the project by default. If you need a dimens. xml , you can just create one under the res/values folder with New -> Values resource file . true but there what is the cause of it and how to get it created automatically when the project is created.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With