1

Is there any way to change a R.string programmatically? Because it's throwing an error.

Basically I want to do this: String parkAdd = getString(R.string.stg_ParkAddress_+id);

Because I have hardcoded strings that are changed according ID.

I tried to do this but don't work:

String parkAdd = getString(R.string.stg_ParkAddress_1);
parkAdd = parkAdd.replace("1",id);
if (!parkAdd.equalsIgnoreCase("")){
    tvParkAddress.setText(parkAdd);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
FilipeOS
  • 801
  • 1
  • 11
  • 42

4 Answers4

5

The R.string.xxx is actually a constant and the value can't be appended to, the resource will never be found. You can search for R.java to see the values for your app:

public static final class string {
     public static final int about_open_source_heading=0x7f060013;
     public static final int about_open_source_list=0x7f060014;
}

If you have hard coded strings that depend on a specific value, maybe you can do something like this:

switch ( id ) {
   case 12345: 
      parkAddr = R.string.stg_ParkAddress_12345;
      break;
   case 12346: 
      parkAddr = R.string.stg_ParkAddress_12346;
      break;
}
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • Gary Bak, why are you accessing the `public static final class string`? – FilipeOS Sep 01 '16 at 11:37
  • @FilipeOS - that was just a sample of what the `R.java` file looks like. When you use the constants R.string.stg_ParkAddress_X you are referencing the final in that file/class. – Gary Bak Sep 01 '16 at 11:41
2

String resource can not be changed at runtime. You can save the string in SharedPreference which you can modify and save for further uses.

Kundan
  • 59
  • 7
1

It is not possible to edit the string resources programmatically,Do another class and have your strings placed in this class and access during run time.

0

Simply concatenate it with id

String parkAdd = getString(R.string.stg_ParkAddress) + id;
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
Ibrahim Gharyali
  • 544
  • 3
  • 22