Java Code Examples for android.content.Context#createConfigurationContext()

The following examples show how to use android.content.Context#createConfigurationContext() . 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: LocaleManager.java    From LanguageTest with MIT License 8 votes vote down vote up
private Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Utility.isAtLeastVersion(N)) {
        setLocaleForApi24(config, locale);
        context = context.createConfigurationContext(config);
    } else if (Utility.isAtLeastVersion(JELLY_BEAN_MR1)) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}
 
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: CommonUtil.java    From QuickNote with Apache License 2.0 6 votes vote down vote up
public static void changeLocalLanguage(Context context, Locale locale) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocale(locale);
        config.setLocales(localeList);
        context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    resources.updateConfiguration(config, dm);//所有的系统都需要调用该API
}
 
Example 4
Source File: LocaleUtils.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
public static Context wrapContext(Context context) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(getLocale());
        LocaleList localeList = getLocales();
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else {
        configuration.setLocale(getLocale());
        context = context.createConfigurationContext(configuration);

    }

    return new ContextWrapper(context);
}
 
Example 5
Source File: LocaleHelper.java    From MusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}
 
Example 6
Source File: BrowserView.java    From AndroidProject with Apache License 2.0 5 votes vote down vote up
/**
 * 修复原生 WebView 和 AndroidX 在 Android 5.x 上面崩溃的问题
 */
public static Context getFixedContext(Context context) {
    // 博客地址:https://blog.csdn.net/qq_34206863/article/details/103660307
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // 不用上下文
        return context.createConfigurationContext(new Configuration());
    }
    return context;
}
 
Example 7
Source File: LocalManageUtil.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Context updateResources(Context context, Locale locale) {
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}
 
Example 8
Source File: LanguageUtil.java    From switch_language_sample with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationResources(Context context, String language) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    Locale locale;
    if (TextUtils.isEmpty(language)) {//如果没有指定语言使用系统首选语言
        locale = SupportLanguageUtil.getSystemPreferredLanguage();
    } else {//指定了语言使用指定语言,没有则使用首选语言
        locale = SupportLanguageUtil.getSupportLanguage(language);
    }
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}
 
Example 9
Source File: AppSettings.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
public static Context loadLocale( Context context, String languageTag )
{
    if (systemLocale == null) {
        systemLocale = Locale.getDefault().getLanguage();
    }

    Locale customLocale = localeForLanguageTag(languageTag);
    Locale.setDefault(customLocale);
    Log.i("loadLocale", languageTag);

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

    if (Build.VERSION.SDK_INT >= 17)
        config.setLocale(customLocale);
    else config.locale = customLocale;

    if (Build.VERSION.SDK_INT >= 25) {
        return new ContextWrapper(context.createConfigurationContext(config));

    } else {
        DisplayMetrics metrics = resources.getDisplayMetrics();
        //noinspection deprecation
        resources.updateConfiguration(config, metrics);
        return new ContextWrapper(context);
    }
}
 
Example 10
Source File: GameBaseActivity.java    From kcanotify_h5-master with GNU General Public License v3.0 5 votes vote down vote up
private void updateLanguage(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    resources.updateConfiguration(config, dm);
}
 
Example 11
Source File: LocaleUtils.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Context updateResources(Context context, String language) {
    Locale locale = fetchLocaleFromString(language);
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}
 
Example 12
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 13
Source File: LocaleHelper.java    From Change-Language-AtRuntime with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}
 
Example 14
Source File: LocaleHelper.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    return context.createConfigurationContext(configuration);
}
 
Example 15
Source File: PictureLanguageUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * set default language
 *
 * @param context
 */
private static void setDefaultLanguage(Context context) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(config.locale);
        context.createConfigurationContext(config);
    }
    resources.updateConfiguration(config, dm);
}
 
Example 16
Source File: PermissionDialogActivity.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {

    Resources resources = context.getResources();
    Locale locale = LauncherApplication.getInstance().getLanguageLocale();

    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(locale);
    configuration.setLocales(new LocaleList(locale));
    return context.createConfigurationContext(configuration);
}
 
Example 17
Source File: LanguageUtil.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * android7.0之后,需要set local 到 configuration
 * @param context 要set的Context
 * @param language 要替换的语言
 * @return 一个通过configuration创建的新的Context
 */
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationContext(Context context, String language) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    Locale locale = getSupportLanguage(language);
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}
 
Example 18
Source File: ConfigurationUtils.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
@NonNull
private static Context createContextAndroidN(@NonNull Context context, @NonNull Locale newLocale) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(newLocale);
    return context.createConfigurationContext(configuration);
}
 
Example 19
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 20
Source File: LocaleHelper.java    From xmrwallet with Apache License 2.0 3 votes vote down vote up
public static Context setLocale(Context context, String locale) {
    setPreferredLocale(context, locale);

    Locale newLocale = (locale.isEmpty()) ? SYSTEM_DEFAULT_LOCALE : Locale.forLanguageTag(locale);
    Configuration configuration = context.getResources().getConfiguration();

    Locale.setDefault(newLocale);

    configuration.setLocale(newLocale);
    configuration.setLayoutDirection(newLocale);

    return context.createConfigurationContext(configuration);
}