0



I need to make some modification to the strings defined in strings.xml before displaying it to the View. I am able to extend Resources class and override the getString() function to return the modified string.

public class MyResource extends Resources {
    public MyResource(AssetManager assets, DisplayMetrics metrics,
        Configuration config) {
        super(assets, metrics, config);
    }

    @Override
    public String getString(int id) throws NotFoundException {
        if(super.getResourceEntryName(id).equals("hello_world"))
            return super.getString(id) + " **";  //modifying string for hello_world
        return super.getString(id);
    }
}

And use it in the application as :

MyResource myres = new MyResource(super.getAssets(), super.getResources().getDisplayMetrics(), super.getResources().getConfiguration());
String modStr = myres.getString(R.string.hello_world); // returns the modified string
textview.setText(modStr);

This works fine, but when I define the text in the layout file and inflate that layout, this piece of code doesn't have any effect and the string is displayed as is.

setContentView(R.layout.activity_main);

inside activity_main layout :

<TextView
    android:id="@+id/helloText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textColor="#ff0000" />

I do not want to explicitly set the text for each view (as mentioned above) and would like to achieve this with layout xml as well. Is there a generic method such that in either way my overridden Resources is called? Or, any other way of achieving this?

Thanks in advance.

Rahul
  • 91
  • 1
  • 8
  • 1
    I dont think i was necessary to extend the Resources class... why you did it that way???? – ΦXocę 웃 Пepeúpa ツ Feb 16 '16 at 20:37
  • Why can't you just adjust the text dynamically in your code? Like just call the String resource and then just add to it whatever you want before displaying it. I'm having trouble understanding why that wouldn't work for you. – NoChinDeluxe Feb 16 '16 at 21:54
  • 1
    @NoChinDeluxe : I will have a modified strings.xml (possibly downloaded over the network) which I will parse and would now show the modified translations for the strings mentioned in that strings.xml. This strings.xml will have only the subset of strings which are modified. Since I do not know in advance which strings are being changed, I have no way of handling it in the code. This will be useful for localized strings for which I didn't had the translations available when I shipped the apk. – Rahul Feb 17 '16 at 03:02
  • My question is more about changing the resources on the fly, like replacing a strings.xml and since android doesn't support this natively, I am looking for options to replicate this behavior. – Rahul Feb 17 '16 at 03:07

1 Answers1

1

You're better off using formatted strings in your resources

<string name="test_string">hello world%s</string>

Then call it with

getString(R.string.test_string, " **");

Nice, simple and built in

keith
  • 3,105
  • 2
  • 29
  • 34
  • I might want to replace the entire string instead of just appending the characters. Using formatted strings will not solve the problem. @keith Is there a way of invoking getString() while inflating a layout – Rahul Feb 17 '16 at 02:33
  • If you're replacing the entire string then use a different string resource at that point. I'm still leaning towards string resources to solve your issue as they are best practice when displaying text to a user in an Android application – keith Feb 19 '16 at 21:37
  • I am trying to create a library that will change the text on android app. The text to be changed is not predefined and the string resources for that doesn't exists while compiling the app. I can handle the strings rendered using setText (by making a wrapper class and using it to set the text). **Could you suggest how do I handle the strings defined in layout file?** – Rahul Feb 28 '16 at 02:23