1

I have a code like this

if(POS>=5){
    Toast.makeText(SubActivity.this,R.string.last,Toast.LENGTH_SHORT).show();
 }else {
     POS += 1;
     fillDetails(POS);
 }

I want to get this "5" in if(POS>=5) from strings.xml

In strings.xml I have

<string name="lastarticle">5</string>

I have tried these

if(POS>=R.string.lastaricle)

if(POS>=getString(R.string.lastaricle))

if(POS>=getResources().getString(R.string.mess_1))

if(POS>=this.getString(R.string.resource_name))

if(POS>=@string/lastarticle)

but none of them worked. I think I miss something but I don't know what it is?

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Jane S.
  • 65
  • 1
  • 7
  • 1
    Your can do whats in the answer but you should do this https://stackoverflow.com/questions/19297522/android-integer-from-xml-resource – Rohit5k2 Jan 10 '20 at 13:38
  • 1
    You trying to do something like this `if(integer >= string)` which makes no sense. – oxyt Jan 10 '20 at 13:38

2 Answers2

2

You should convert that string to int and then compare it with POS:

if(POS >= Integer.parseInt(getString(R.string.lastaricle))){
     Toast.makeText(SubActivity.this, R.string.last, Toast.LENGTH_SHORT).show();
} else {
     POS += 1;
     fillDetails(POS);
}
Masoud Maleki
  • 813
  • 6
  • 13
2

Use integers.xml for ints ref : https://developer.android.com/guide/topics/resources/more-resources#Integer

And then no matter if you use strings.xml or integers.xml values are generated ints that are reference to i.e. R.string.lastaricle or R.integer.lastaricle so to get value you need call getter from https://developer.android.com/reference/android/content/res/Resources

if(POS>=getString(R.string.lastaricle))

if(POS>=getResources().getString(R.string.mess_1))

if(POS>=this.getString(R.string.resource_name))

Those didn't work cause you compared String with Integer

integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="lastaricle">5</integer>
</resources>
    if (POS >= getResources().getInteger(R.integer.lastaricle)) {
        Toast.makeText(SubActivity.this, R.string.lastaricle, Toast.LENGTH_SHORT).show();
    } else {
        POS += 1;
        fillDetails(POS);
    }
Community
  • 1
  • 1
Kamil
  • 467
  • 2
  • 13