In this small blog, we will see the basic solution to solve the Android 7.1 issue, somehow android nougat 7.1 devices automatically resets application locale after launching the WebView.
In the below solution, we will change the locale to ES(Spanish language).
How to fix
Step 1: Add this link before or after loading the webview,
1 |
MobikulApplication.getApplication().changeIntoEsLocale(); |
like this,
1 2 3 4 5 |
String desc_data = getArguments().getString("data"); String mime = "text/html"; String encoding = "utf-8"; MobikulApplication.getApplication().changeIntoEsLocale(); ((WebView) view.findViewById(R.id.desc_data)).loadData(desc_data, mime, encoding); |
Step 2: Create a method for language change in application Class,
1 2 3 4 5 6 7 8 9 10 11 |
public void changeIntoEsLocale() { Locale mLocale = new Locale("es"); Configuration config = getBaseContext().getResources() .getConfiguration(); Locale.setDefault(mLocale); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(mLocale); } else config.locale = mLocale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } |
Step 3: Call it to the onCreate(),
1 2 3 4 5 6 |
@Override public void onCreate() { super.onCreate(); changeIntoEsLocale(); sApplication = this; } |
complete application class,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class MobikulApplication extends Application { private static MobikulApplication sApplication; @Override public void onCreate() { super.onCreate(); changeIntoEsLocale(); sApplication = this; } public static MobikulApplication getApplication() { return sApplication; } public void changeIntoEsLocale() { Locale mLocale = new Locale("es"); Configuration config = getBaseContext().getResources() .getConfiguration(); Locale.setDefault(mLocale); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(mLocale); } else config.locale = mLocale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } } |
If you have any doubt on this blog, please ask in comments. And stay updated and stay super.
Reference link.