Consider that I have an Rest API that let me fetch a strings.xml file.
Once downloaded is it posible set as the default values/strings.xml file of the app?
-
Why would you do that. What kind of resources would be shown to user between app start and resource loaded?ror– ror2020-06-02 10:16:36 +00:00Commented Jun 2, 2020 at 10:16
-
Check this answer , it looks like you can not do it from the answerTamir Abutbul– Tamir Abutbul2020-06-02 10:26:27 +00:00Commented Jun 2, 2020 at 10:26
Add a comment
|
1 Answer
String resources are static defined and can not be changed at runtime.
However, you could use the Restring library to set and load strings at runtime by adding the dependencies:
// Replace bundled strings dynamically
implementation 'dev.b3nedikt.restring:restring:4.0.3'
// Intercept view inflation
implementation 'io.github.inflationx:viewpump:2.0.3'
// Allows to update the text of views at runtime without recreating the activity
implementation 'dev.b3nedikt.reword:reword:1.1.0'
Initialising it in your Application class:
Restring.init(this,
new RestringConfig.Builder()
.stringsLoader(new SampleStringsLoader())
.loadAsync(false) // If string loader should load strings asynchronously, default true
.build()
);
ViewPump.init(ViewPump.builder()
.addInterceptor(RewordInterceptor.INSTANCE)
.build());
Then it should be injected into context:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase)));
}
@Override
public Resources getResources() {
return Restring.wrapContext(getBaseContext()).getResources();
}
And you can load more strings at any time:
// e.g. locale=Locale.EN newStrings=map of (key-value)s
Restring.setStrings(locale, newStrings);
And apply updated resources without restarting the app:
// The layout containing the views you want to localise
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
Reword.reword(rootView);
More details: https://github.com/B3nedikt/restring