anddev
anddev

Reputation: 3204

Change default phone language by changing application language in Android?

Is there any way to change language of phone by changing the language from application.

I mean when I change the language of my application then the default phone language will also change.

Is there any idea about this then please share here.

Thanks in advance.

Upvotes: 1

Views: 1713

Answers (2)

Sharmilee
Sharmilee

Reputation: 1295

add android:configChanges="locale" to your activity decalaration in AndroidManifect file.

Then call following method from onCreate of that activity .

public static void setLanguage(Context context, String languageToLoad) {
  Log.d(TAG, "setting language");
  Locale locale = new Locale(languageToLoad); //e.g "sv"
  Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
   if (systemLocale != null && systemLocale.equals(locale)) {
   Log.d(TAG, "Already correct language set");
   return;
 }
 Locale.setDefault(locale);
 android.content.res.Configuration config = new android.content.res.Configuration();
 config.locale = locale;
 context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());                                                            
 Log.d(TAG, "Language set");
  }

Upvotes: 1

Talha
Talha

Reputation: 12717

I dont know that it can be changed programatically, but after you changed your app language you can ask user to change device language also,

Ask user to change device language

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");            
startActivity(intent);

Change app language

<activity
    android:name=".ui.SomeActivity"
    android:configChanges="locale"
    :
    :
</activity>


public static void setLanguage(Context context, String languageToLoad) {
    Log.d(TAG, "setting language");
    Locale locale = new Locale(languageToLoad); //e.g "sv"
    Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
    if (systemLocale != null && systemLocale.equals(locale)) {
       Log.d(TAG, "Already correct language set");
       return;
    }
    Locale.setDefault(locale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Log.d(TAG, "Language set");
}

Upvotes: 1

Related Questions