JasonDD
JasonDD

Reputation: 23

Can't access integer in xml file?

xml file in res/xml/sizes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name = "size1">0</integer>
    <integer name = "size2">1</integer>
    <integer name = "size3">2</integer>
</resources>

In my java file, I'm trying to access the integer by doing

private int sizeState = R.xml.sizes.size1;

This gives the error:

Cannot resolve symbol 'size1'

Upvotes: 0

Views: 196

Answers (2)

Danh DC
Danh DC

Reputation: 1246

To access interger value from xml file, do like this :

Resources res = getResources();
int sizeState = res.getInteger(R.integer.size1);

Please make sure your file location in : res/values/sizes.xml

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

Upvotes: 4

Jas
Jas

Reputation: 3212

Try this:

  int size= this.getResources().getInteger(R.integer.size1);

Upvotes: 0

Related Questions