Change Value of R.String Programmatically

Changing value of R.String Programmatically

You can't change strings.xml dynamically since it's a compiled resource. There are other mechanisms for saving data in Android, here's a nice post that covers this topic: Data Storage. Hope this helps.

Change value of R.string programmatically

One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.

This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time. This is the best solution what I have found.

Change value of R.string programmatically?

If you want to change current language for you app you can do it by using standard built-in localization features and changing locale programatically.

Change Resource getString() programmatically

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;
}

Android: Change strings resource Programmatically

You can update the label on TextView or anything similar.

  • Get the updated string using update_str = Context.getResources().getString (R.string.update_identifier)
  • Update the label using ((TextView)findViewById (R.id.textview)).setText (updated_str)

the right way to set text in TextView programmatically

points_txt.setText(getResources().getString(R.string.you_have) + current_points + getResources().getString(R.string.points));


Related Topics



Leave a reply



Submit