Skip to content Skip to sidebar Skip to footer

Is It Possible To Update String.xml File At Run Time In Android?

I wanted to apply localization in my app dynamically so is it possible to define strings in java file and fetch that strings to our layout xml file i.e as we do @string/anystringna

Solution 1:

Basically, No.

You cannot manipulate resource file dynamically. You need to implement such functionality on your own.

Here is an example:

english.json

{"hello":"Hello"}

french.json

{"hello":"Bonjour"}
publicclassStringManager {
    privatestaticStringManagersInstance=null;
    privatestaticStringsDefaultLanguageCode="en";

    private Map<String, JSONObject> mLanguageMap = newArrayMap<>();

    privateStringManager() {
    }

    publicstatic StringManager getInstance() {
        if (sInstance == null) {
            synchronized (StringManager.class) {
                if (sInstance == null)
                    sInstance = newStringManager();
            }
        }
        return sInstance;
    }

    publicstaticvoidsetDefaultLanguageCode(String languageCode) {
        sDefaultLanguageCode = languageCode;
    }

    publicvoidaddLanguage(String languageCode, JSONObject json) {
        mLanguageMap.put(languageCode, json);
    }

    public String getString(String key) {
        return mLanguageMap.get(mDefaultLanguageCode).getString(key);
    }

    public String getString(String languageCode, String key) {
        return mLanguageMap.get(languageCode).getString(key);
    }
}

publicclassSomeActivityextendsActivity {
    ...
    publicvoidinitStringResources() {
        // assume englishJsonObject is created from english.json
        StringManager.getInstance().addLanguage("en", englishJsonObject);
        StringManager.getInstance().addLanguage("fr", frenchJsonObject);

        StringManager.setDefaultLanguageCode("fr");
    }

    publicvoiduseSomeString() {
        StringhelloString= StringManager.getInstance().getString("hello");
        // helloString will be "Bonjour"// and you can get specific language stringStringenglishHelloString= StringManager.getInstance().getString("en", "hello");
    }

    // this may be called from Button or other UI componentpublicvoidonLanguageChanged(String languageCode) {
        StringManager.setDefaultLanguageCode(languageCode);
        // and update UI components that display localized strings
    }
}

Solution 2:

This is not possible. It is however possible to change the language of your app at runtime. This allows you the switch between the strings.xml for the different languages. A library I used for this is the Android localization library All your activities have to extend the LocalizationActivity which extends the AppCompatActivity. You just have to call setLanguage("en"); To change your apps language, and use a different strings.xml

In this thread you can read more possible solutions.

Solution 3:

<TextView  
android:id="@+id/tv"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/tv"
/>

<string name="tv">old text</string>//old textfinal TextView textView = (TextView) findViewById(R.id.rate);
textView.setText("new text");//new text

Post a Comment for "Is It Possible To Update String.xml File At Run Time In Android?"