How does one specify an integer value in xml
and retrieve that value in Android
application?
Yes it is possible, it would look like this: Create an xml resources file in the folder /res/values/ called integers. xml.
A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android: dp.
The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.
Resources are used for anything from defining colors, images, layouts, menus, and string values. The value of this is that nothing is hardcoded. Everything is defined in these resource files and then can be referenced within your application's code.
Do as follows:
Create integers.xml file in your project's \res\values
folder. In the file create your integer
:
<resources>
<integer name="your_integer">value</integer>
</resources>
Use it as xml attribute. For example for TextView
:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="@integer/your_integer" />
Or use it programmatically
int myint = getResources().getInteger(R.integer.your_integer);
Reference: Integer resources
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