Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define and use an integer value in Android resources?

How does one specify an integer value in xml and retrieve that value in Android application?

like image 595
user3617132 Avatar asked May 08 '14 15:05

user3617132


People also ask

Can integers be stored as resources in XML?

Yes it is possible, it would look like this: Create an xml resources file in the folder /res/values/ called integers. xml.

How does Android define Dimen?

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.

What is the use of values in Android Studio?

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.

What is resource value in Android Studio?

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.


1 Answers

Do as follows:

  1. 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>
    
  2. 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" />
    
  3. Or use it programmatically

    int myint = getResources().getInteger(R.integer.your_integer);
    

Reference: Integer resources

like image 62
Onik Avatar answered Oct 16 '22 22:10

Onik