Skip to main content
deleted 1 character in body
Source Link

You can ask the user to select the language in first screen and save it in SharedPreferences

SharedPreferences.Editor editor = getSharedPreferences("uinfo""data", MODE_PRIVATE).edit(); editor.putString("lang", "si"); editor.apply(); recreate(); 

Then you can take it in every Activity in your application. Here I have set English and Sinhala languages.

@Override protected void attachBaseContext(Context base) { SharedPreferences prefs = base.getSharedPreferences("uinfo", MODE_PRIVATE); String restoredText = prefs.getString("lang", "No name defined"); if (restoredText.equals("si")){ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "si")); }else{ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "en")); } } 

And this is your localUpdateResources method. Place it in LocalHelper class

public class LocaleHelper { public static Context localeUpdateResources(Context context, String languageCode) { Context newContext = context; Locale locale = new Locale(languageCode); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration config = new Configuration(resources.getConfiguration()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(locale); newContext = context.createConfigurationContext(config); } else { config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); } return newContext; } } 

You can ask the user to select the language in first screen and save it in SharedPreferences

SharedPreferences.Editor editor = getSharedPreferences("uinfo", MODE_PRIVATE).edit(); editor.putString("lang", "si"); editor.apply(); recreate(); 

Then you can take it in every Activity in your application. Here I have set English and Sinhala languages.

@Override protected void attachBaseContext(Context base) { SharedPreferences prefs = base.getSharedPreferences("uinfo", MODE_PRIVATE); String restoredText = prefs.getString("lang", "No name defined"); if (restoredText.equals("si")){ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "si")); }else{ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "en")); } } 

And this is your localUpdateResources method. Place it in LocalHelper class

public class LocaleHelper { public static Context localeUpdateResources(Context context, String languageCode) { Context newContext = context; Locale locale = new Locale(languageCode); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration config = new Configuration(resources.getConfiguration()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(locale); newContext = context.createConfigurationContext(config); } else { config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); } return newContext; } } 

You can ask the user to select the language in first screen and save it in SharedPreferences

SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("lang", "si"); editor.apply(); recreate(); 

Then you can take it in every Activity in your application. Here I have set English and Sinhala languages.

@Override protected void attachBaseContext(Context base) { SharedPreferences prefs = base.getSharedPreferences("uinfo", MODE_PRIVATE); String restoredText = prefs.getString("lang", "No name defined"); if (restoredText.equals("si")){ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "si")); }else{ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "en")); } } 

And this is your localUpdateResources method. Place it in LocalHelper class

public class LocaleHelper { public static Context localeUpdateResources(Context context, String languageCode) { Context newContext = context; Locale locale = new Locale(languageCode); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration config = new Configuration(resources.getConfiguration()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(locale); newContext = context.createConfigurationContext(config); } else { config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); } return newContext; } } 
Source Link

You can ask the user to select the language in first screen and save it in SharedPreferences

SharedPreferences.Editor editor = getSharedPreferences("uinfo", MODE_PRIVATE).edit(); editor.putString("lang", "si"); editor.apply(); recreate(); 

Then you can take it in every Activity in your application. Here I have set English and Sinhala languages.

@Override protected void attachBaseContext(Context base) { SharedPreferences prefs = base.getSharedPreferences("uinfo", MODE_PRIVATE); String restoredText = prefs.getString("lang", "No name defined"); if (restoredText.equals("si")){ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "si")); }else{ super.attachBaseContext(LocaleHelper.localeUpdateResources(base, "en")); } } 

And this is your localUpdateResources method. Place it in LocalHelper class

public class LocaleHelper { public static Context localeUpdateResources(Context context, String languageCode) { Context newContext = context; Locale locale = new Locale(languageCode); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration config = new Configuration(resources.getConfiguration()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { config.setLocale(locale); newContext = context.createConfigurationContext(config); } else { config.locale = locale; resources.updateConfiguration(config, resources.getDisplayMetrics()); } return newContext; } }