Search code examples
androidmultilingual

Change language for whole Android application activities from Setting activity


I'm trying to build a very simple application to test the multi language subject, the application should also recognize the language of the device , but also there is a way to change the language from another activity.

I'm using the languages folders, so if the user language is suite to one of the supported languages, it will take the right one.

The problem start when I'm trying to change it from 'Setting' activity.

I have a spinner that include the supported languages, after selecting new language, the current activity should be close and the language of both activities should be changed.

I saw a couple of examples on web' but no one help me here.

This is my XML code (just include a sippner and text):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.MES.yo.ActLanguageSettings">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="@string/Choose_Language" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/change_language" />

    <Spinner
        android:id="@+id/spnlanguage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

This is the code for changing the language:

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());
    }

    private 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();
    }

And for each activity I'm calling to "onConfigurationChanged" method like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    Locale locale = new Locale(GVC.getLanguage());
    newConfig.locale = locale;
    super.onConfigurationChanged(newConfig);
    Locale.setDefault(locale);
    getBaseContext().getResources().updateConfiguration(newConfig,getBaseContext().getResources().getDisplayMetrics());
}

The Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.MES.yo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />

    <application
        android:name=".GlobalVarClass"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:configChanges="locale">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActLanguageSettings"
            android:label="@string/title_activity_act_language_settings" 
            android:configChanges="locale">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.MES.MySperm.MainActivity" />
        </activity>
    </application>

</manifest>

This is doesn't work at all for me.

I tried also to change the language like this:

public void setLocale(String lang) {
            GVC.setLanguage(lang);
            myLocale = new Locale(lang);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(this, ActLanguageSettings.class);
            startActivity(refresh);
            finish();
    }

here the language was changed, but not for all other activities, Is there a rule of thumb for how to do it right?


Solution

  • Try this code to change the language...

                        Locale locale = new Locale("ar");
                        Locale.setDefault(locale);
                        Configuration config = new Configuration();
                        config.locale = locale;
                        getBaseContext().getResources().updateConfiguration(config,
                                getBaseContext().getResources().getDisplayMetrics());
                        settings.edit().putString("locale", "ar").commit();
                        this.finish();
                        Intent refresh = new Intent(this, MainActivity.class);
                        startActivity(refresh);
    

    And in each activity's onCreate and Onresume Methods you have to do somthing like this..

    if (settings.getString("locale","").equals("en")) {
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            settings.edit().putString("locale", "en").commit();
        }