Java Code Examples for android.content.res.Resources#updateConfiguration()

The following examples show how to use android.content.res.Resources#updateConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalManageUtil.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 设置语言类型
 */
public static void setApplicationLanguage(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getSetLanguageLocale(context);
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
Example 2
Source File: SwitchLocaleTest.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static void changeLocale(String locale) {
    Context context = InstrumentationRegistry.getInstrumentation()
            .getTargetContext();

    Resources res = context.getApplicationContext().getResources();
    Configuration config = res.getConfiguration();


    config.setLocale(new Locale(locale));
    if (SDK_INT >= 25) {
        context.createConfigurationContext(config);
    } else {
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}
 
Example 3
Source File: WifiConnectActivity.java    From Lamp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void checkLanguage() {

        SharedPreferences sharedPreferences = this.getSharedPreferences("set", MODE_PRIVATE);
        /**
         * 获取系统配置
         */
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();


        if (LANGUAGE_CN.equals(sharedPreferences.getString("language", "0"))) {

            config.locale = Locale.SIMPLIFIED_CHINESE;
            resources.updateConfiguration(config, dm);
        } else if (LANGUAGE_EN.equals(sharedPreferences.getString("language", "0"))) {

            config.locale = Locale.ENGLISH;
            resources.updateConfiguration(config, dm);
        } else {

            config.locale = Locale.SIMPLIFIED_CHINESE;
            resources.updateConfiguration(config, dm);
        }

    }
 
Example 4
Source File: AppUtils.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static void updateResourcesLegacy(Context context, String language) {
    Locale locale = getLocale(language);
    Locale.setDefault(locale);
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
 
Example 5
Source File: MainActivity.java    From MaterialPreference with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_night_mode:
            /*AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES ?
                    AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);*/

            recreate();

            return true;
        case R.id.rtl:
            Resources resources = getBaseContext().getResources();

            Locale locale = resources.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
                    ? Locale.ENGLISH : new Locale("ar");
            Locale.setDefault(locale);

            resources.getConfiguration().setLocale(locale);
            resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());

            startActivity(new Intent(this, this.getClass()));
            finish();

            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 6
Source File: AlbumUtils.java    From Album with Apache License 2.0 5 votes vote down vote up
/**
 * Setting {@link Locale} for {@link Context}.
 *
 * @param context to set the specified locale context.
 * @param locale  locale.
 */
@NonNull
public static Context applyLanguageForContext(@NonNull Context context, @NonNull Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return context;
    }
}
 
Example 7
Source File: AppUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 改变语言环境
 *
 * @param resources
 * @param lanAtr
 */
public static void changeAppLanguage(Resources resources, String lanAtr) {
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (lanAtr.equals("zh-cn")) {
        config.locale = Locale.SIMPLIFIED_CHINESE;
    } else {
        config.locale = Locale.getDefault();
    }
    resources.updateConfiguration(config, dm);
}
 
Example 8
Source File: MultiLanguageUtil.java    From AndroidMultiLanguage with Apache License 2.0 5 votes vote down vote up
/**
 * 设置语言
 */
public void setConfiguration() {
    Locale targetLocale = getLanguageLocale();
    Configuration configuration = mContext.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(targetLocale);
    } else {
        configuration.locale = targetLocale;
    }
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
}
 
Example 9
Source File: Utils.java    From SmartFlasher with GNU General Public License v3.0 5 votes vote down vote up
public static void setLanguage(Context context) {
    Locale myLocale = new Locale(getLanguage(context));
    Resources res = context.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}
 
Example 10
Source File: Application.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a ContextWrapper, wrappint the context with a specific locale.
 *
 * @param context The original context.
 * @return The context wrapper.
 */
public static ContextWrapper createContextWrapperForLocale(final Context context) {
	Resources res = context.getResources();
	Configuration configuration = res.getConfiguration();
	Locale newLocale = getApplicationLocale();
	Context newContext = context;

	if (VERSION.SDK_INT >= VERSION_CODES.N) {
		configuration.setLocale(newLocale);

		LocaleList localeList = new LocaleList(newLocale);
		LocaleList.setDefault(localeList);
		configuration.setLocales(localeList);

		newContext = context.createConfigurationContext(configuration);

	}
	else if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
		configuration.setLocale(newLocale);
		newContext = context.createConfigurationContext(configuration);

	}
	else {
		configuration.locale = newLocale;
		res.updateConfiguration(configuration, res.getDisplayMetrics());
	}
	return new ContextWrapper(newContext);
}
 
Example 11
Source File: MyApplication.java    From openshop.io-android with MIT License 5 votes vote down vote up
/**
 * Method sets app specific language localization by selected shop.
 * Have to be called from every activity.
 *
 * @param lang language code.
 */
public static void setAppLocale(String lang) {
    Resources res = mInstance.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang);
    Timber.d("Setting language: %s", lang);
    res.updateConfiguration(conf, dm);
}
 
Example 12
Source File: LocaleUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
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());
    }
}
 
Example 13
Source File: ProxyFragment.java    From letv with Apache License 2.0 5 votes vote down vote up
private void updateConfiguration() {
    if (JarApplication.getInstance() != null && JarApplication.getInstance().getResources() != null) {
        Resources superRes = JarApplication.getInstance().getResources();
        Configuration configuration = superRes.getConfiguration();
        DisplayMetrics displayMetrics = superRes.getDisplayMetrics();
        JarResources jarResources = getOverrideResources();
        if (jarResources != null) {
            Resources resources = jarResources.getResources();
            if (resources != null && configuration != null && displayMetrics != null) {
                resources.updateConfiguration(configuration, displayMetrics);
            }
        }
    }
}
 
Example 14
Source File: MyApplication.java    From SimpleProject with MIT License 5 votes vote down vote up
/**
 * 放大系统字体将导致布局错乱,所以一般可将其屏蔽
 * @return
 */
@Override
public Resources getResources() {
	Resources res = super.getResources();
	if (res.getConfiguration().fontScale != 1) {
		Configuration newConfig = new Configuration();
		newConfig.setToDefaults();
		res.updateConfiguration(newConfig, res.getDisplayMetrics());
	}

	return res;
}
 
Example 15
Source File: LocaleManager.java    From UpdogFarmer with GNU General Public License v3.0 5 votes vote down vote up
private static Context updateResources(Context context, String language) {
    final Locale locale = forLanguageTag(language);
    Locale.setDefault(locale);

    final Resources res = context.getResources();
    final Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}
 
Example 16
Source File: ThemeUtils.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }
    return resource;
}
 
Example 17
Source File: FrmApplication.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resources getResources() {
    Resources res = super.getResources();
    if (res.getConfiguration().fontScale != 1) {//非默认值
        Configuration newConfig = new Configuration();
        newConfig.setToDefaults();//设置默认
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
    }
    return res;
}
 
Example 18
Source File: PrefsUtility.java    From RedReader with GNU General Public License v3.0 5 votes vote down vote up
private static void applyLanguage(final Activity activity, final SharedPreferences prefs) {

		final String lang = getString(R.string.pref_appearance_langforce_key, "auto", activity, prefs);

		for(final Resources res : new Resources[] {
			activity.getResources(),
			activity.getApplication().getResources()
		}) {

			final DisplayMetrics dm = res.getDisplayMetrics();
			final android.content.res.Configuration conf = res.getConfiguration();

			if(!lang.equals("auto")) {

				if(lang.contains("-r")) {
					final String[] split = lang.split("-r");
					conf.locale = new Locale(split[0], split[1]);

				} else {
					conf.locale = new Locale(lang);
				}

			} else {
				conf.locale = Locale.getDefault();
			}

			res.updateConfiguration(conf, dm);
		}
	}
 
Example 19
Source File: TestButler.java    From test-butler with Apache License 2.0 3 votes vote down vote up
/**
 * Set the locale for the application under test. Requires API 17+.
 * <p>
 * NOTE: this does not change the device locale! Changing the device locale on the emulator
 * has been slow/unreliable and led to flaky tests when the locale setting did not take effect in time.
 * Instead, this method uses the {@link Configuration} class to update the locale on the {@link Resources}
 * object of the {@link Context} from the application under test. As long as apps are checking the
 * current locale from the current {@link Configuration}, this approach will be reliable.
 *
 * @param language the language code for the new locale, as expected by {@link Locale#Locale(String, String)}
 * @param country  the country code for the new locale, as expected by {@link Locale#Locale(String, String)}
 * @param context  the "target context"; i.e. Context of the app under test (not the test apk context!)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void setLocale(@NonNull String language, @NonNull String country, @NonNull Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        throw new IllegalStateException("Cannot change locale before API 17");
    }

    context = context.getApplicationContext();
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(language, country));
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
 
Example 20
Source File: DynamicLanguageContextWrapper.java    From mollyim-android with GNU General Public License v3.0 3 votes vote down vote up
public static Context updateContext(Context context, String language) {
  final Locale newLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);

  Locale.setDefault(newLocale);

  final Resources     resources = context.getResources();
  final Configuration config    = resources.getConfiguration();
  final Configuration newConfig = copyWithNewLocale(config, newLocale);

  resources.updateConfiguration(newConfig, resources.getDisplayMetrics());

  return context;
}