4

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?

2
  • Why would you do that. What kind of resources would be shown to user between app start and resource loaded? Commented Jun 2, 2020 at 10:16
  • Check this answer , it looks like you can not do it from the answer Commented Jun 2, 2020 at 10:26

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.