Anda di halaman 1dari 2

ANDROID NOUGAT

CHANGE LANGUAGE PROBLEM

Abstract
This document is describing how to solve problem with changing language of application on
Android Nougat

Edin Selimovi
1. Creating class

You will need to create class Android7LocaleWrapper.java with following code:


public class Android7LocaleWrapper extends ContextWrapper {
public Android7LocaleWrapper(Context base) {
super(base);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static ContextWrapper wrap(Context context) {

Resources res = context.getResources();


Locale newLocale = new Locale(((MyApplication)
context.getApplicationContext()).getLanguage());

Configuration configuration = res.getConfiguration();

configuration.setLocale(newLocale);

context = context.createConfigurationContext(configuration);

configuration.setLocale(newLocale);

configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
context = context.createConfigurationContext(configuration);

return new Android7LocaleWrapper(context);


}
}

2. Usage of class

The second step is to use class Android7LocaleWrapper.java in your code in the next way:

- In every activity you will need to add next code:

@Override
protected void attachBaseContext(Context newBase) {
Context context = Android7LocaleWrapper.wrap(newBase);
super.attachBaseContext(context);
}

Anda mungkin juga menyukai