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.