Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Change Language in Android Application Programmatically?

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 18.1k
    Comment on it

    Sometimes We need to give options to the user to change language of application, regardless of the locale device. In this blog we are providing opportunity to the user to change language programmatically by press the button individually. Here are some steps to complete this as like below:

    Step-1: Prepare string resource for the four language like English,French,Russian and German.

    <resources>
        <string name="app_name">ChangeLocaleExample</string>    
        <string name="hello_world">Hello World</string>
        <string name="btn_en">English</string>
        <string name="btn_ru">Russian</string>
        <string name="btn_de">German</string>
        <string name="btn_fr">French</string>
    </resources>
    

    Source code values-ru/strings.xml (Russian)

    <resources>
        <string name="hello_world">, </string>
        <string name="btn_en"></string>
        <string name="btn_ru"></string>
        <string name="btn_de"></string>
        <string name="btn_fr"></string>
    </resources>
    
    Source code values-fr/strings.xml (French)
    <resources>   
        <string name="hello_world">Bonjour monde</string>
        <string name="btn_en">Anglais</string>
        <string name="btn_ru">Russie</string>
        <string name="btn_de">Allemand</string>
        <string name="btn_fr">Français</string>
    </resources>
    
    Source code values-de/strings.xml (German)
    <resources>   
        <string name="hello_world">Hallo Welt</string>
        <string name="btn_en">Englisch</string>
        <string name="btn_ru">Russisch</string>
        <string name="btn_de">Deutsch</string>
        <string name="btn_fr">Französisch</string>
    </resources>
    

    Step-2: Create main resource xml file which will show four buttons that change the language of the application and the text box to display the greeting line:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/txt_hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/hello_world" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:gravity="center">
    
            <Button
                android:id="@+id/btn_en"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_en" />
    
            <Button
                android:id="@+id/btn_ru"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_ru" />
    
            <Button
                android:id="@+id/btn_de"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_de" />
    
            <Button
                android:id="@+id/btn_fr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_fr" />
        </LinearLayout>
    
    </LinearLayout>
    

    Step-3 : This is the final MainActivity.class which implement complete code as below:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private TextView txt_hello;
        private Button btn_en, btn_ru, btn_fr, btn_de;
        private Locale myLocale;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            this.txt_hello = (TextView)findViewById(R.id.txt_hello);
            this.btn_en = (Button) findViewById(R.id.btn_en);
            this.btn_ru = (Button) findViewById(R.id.btn_ru);
            this.btn_fr = (Button)findViewById(R.id.btn_fr);
            this.btn_de = (Button)findViewById(R.id.btn_de);
    
            this.btn_en.setOnClickListener(this);
            this.btn_ru.setOnClickListener(this);
            this.btn_fr.setOnClickListener(this);
            this.btn_de.setOnClickListener(this);
    
            loadLocale();
        }
    
        public void loadLocale()
        {
            String langPref = "Language";
            SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
            String language = prefs.getString(langPref, "");
            changeLang(language);
        }
    
        public void saveLocale(String lang)
        {
            String langPref = "Language";
            SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(langPref, lang);
            editor.commit();
        }
    
        public void changeLang(String lang)
        {
            if (lang.equalsIgnoreCase(""))
                return;
            myLocale = new Locale(lang);
            saveLocale(lang);
            Locale.setDefault(myLocale);
            android.content.res.Configuration config = new android.content.res.Configuration();
            config.locale = myLocale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            updateTexts();
        }
    
        private void updateTexts()
        {
            txt_hello.setText(R.string.hello_world);
            btn_en.setText(R.string.btn_en);
            btn_ru.setText(R.string.btn_ru);
            btn_fr.setText(R.string.btn_fr);
            btn_de.setText(R.string.btn_de);
        }
    
        @Override
        public void onClick(View v) {
            String lang = "en";
            switch (v.getId()) {
                case R.id.btn_en:
                    lang = "en";
                    break;
                case R.id.btn_ru:
                    lang = "ru";
                    break;
                case R.id.btn_de:
                    lang = "de";
                    break;
                case R.id.btn_fr:
                    lang = "fr";
                    break;
                default:
                    break;
            }
            changeLang(lang);
        }
    
        @Override
        public void onConfigurationChanged(android.content.res.Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if (myLocale != null){
                newConfig.locale = myLocale;
                Locale.setDefault(myLocale);
                getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
            }
        }
    }
    

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: