CopyPastor

Detecting plagiarism made easy.

Score: 0.8218687176704407; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2016-04-28
by USKMobility

Original Post

Original - Posted on 2015-08-16
by Roberto B.



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You can change language settings for app only. Use Locale class and update default configuration. Language change only applied when activity restart or re-start application. Use following locale class
public class LocaleUtils { private static Locale sLocale; public static void setLocale(Locale locale) { sLocale = locale; if(sLocale != null) { Locale.setDefault(sLocale); } } public static void updateConfig(ContextThemeWrapper wrapper) { if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Configuration configuration = new Configuration(); configuration.setLocale(sLocale); wrapper.applyOverrideConfiguration(configuration); } } public static void updateConfig(Application app, Configuration configuration) { if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { //Wrapping the configuration to avoid Activity endless loop Configuration config = new Configuration(configuration); config.locale = sLocale; Resources res = app.getBaseContext().getResources(); res.updateConfiguration(config, res.getDisplayMetrics()); } } }
Application class :
public class App extends Application { public void onCreate(){ super.onCreate(); // get user preferred language set locale accordingly new locale(language,country) LocaleUtils.setLocale(new Locale("iw")); LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration()); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); LocaleUtils.updateConfig(this, newConfig); } }
BaseActivity :
public class BaseActivity extends Activity { public BaseActivity() { LocaleUtils.updateConfig(this); } }
Above answer is taken from : https://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself
Android Document : http://developer.android.com/reference/java/util/Locale.html

In Android M the top solution won't work. I've written a helper class to fix that which you should call from your Application class and all Activities (I would suggest creating a BaseActivity and then make all the Activities inherit from it.
**Note**: This will also support properly RTL layout direction.
Helper class:
public class LocaleUtils { private static Locale sLocale; public static void setLocale(Locale locale) { sLocale = locale; if(sLocale != null) { Locale.setDefault(sLocale); } } public static void updateConfig(ContextThemeWrapper wrapper) { if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Configuration configuration = new Configuration(); configuration.setLocale(sLocale); wrapper.applyOverrideConfiguration(configuration); } } public static void updateConfig(Application app, Configuration configuration) { if (sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { //Wrapping the configuration to avoid Activity endless loop Configuration config = new Configuration(configuration); // We must use the now-deprecated config.locale and res.updateConfiguration here, // because the replacements aren't available till API level 24 and 17 respectively. config.locale = sLocale; Resources res = app.getBaseContext().getResources(); res.updateConfiguration(config, res.getDisplayMetrics()); } } }
Application:
public class App extends Application { public void onCreate(){ super.onCreate(); LocaleUtils.setLocale(new Locale("iw")); LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration()); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); LocaleUtils.updateConfig(this, newConfig); } }
BaseActivity:
public class BaseActivity extends Activity { public BaseActivity() { LocaleUtils.updateConfig(this); } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;