Question

In XML I defined two constants like below

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="toast_x">10</integer>
    <integer name="toast_y">100</integer>
</resources>

But when I access in a code like below

int xco = R.integer.toast_x;
int yco = R.integer.toast_y;

I am getting weird value 2131296257 and 2131296258 respectively

I know those are HEX value.

But I just want to access actual value i.e 10 and 100

Please let me know that how can I achieve this....

Was it helpful?

Solution

How to get integer resource value:

int xco = getResources().getInteger(R.integer.toast_x);
int yco = getResources().getInteger(R.integer.toast_y);

OTHER TIPS

actually you need Context too if you trying to access the value from outside Activity

class boo{
   public static void foo(Context con){
      int xco = con.getResources().getInteger(R.integer.toast_x);
      int yco = con.getResources().getInteger(R.integer.toast_y);
   }
}

getResources() function inside Context
so here fun to access value from out side Activity

class boo{
   public static int foo(Context con,int resId){
      return con.getResources().getInteger(resId);
   }
}

Define both toast_x and toast_y in tag of their own.

  <resources>
     <string name="toast_x">10</string>
     <string name="toast_y">100</string>
  </resources>

Now come to your java class and read them as strings and then convert them to integers.

String xcostr = getString(R.string.toast_x);
int xco = Integer.valueOf(xcostr);

String ycostr = getString(R.string.toast_y);
int yco = Integer.valueOf(ycostr);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top