Java Code Examples for android.content.res.Configuration#setLocales()

The following examples show how to use android.content.res.Configuration#setLocales() . 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: 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 2
Source File: LocaleContextWrapper.java    From px-android with MIT License 6 votes vote down vote up
@SuppressWarnings("PMD.AvoidReassigningParameters")
@NonNull
private static Context applyNewLocaleConfig(@NonNull Context context, @NonNull final Locale newLocale) {
    final Configuration newLocaleConfig = context.getResources().getConfiguration();
    final Resources res = context.getResources();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final LocaleList localeList = new LocaleList(newLocale);
        newLocaleConfig.setLocales(localeList);
        newLocaleConfig.setLocale(newLocale);
        context = context.createConfigurationContext(newLocaleConfig);
    } else {
        newLocaleConfig.locale = newLocale;
    }

    Locale.setDefault(newLocale);
    res.updateConfiguration(newLocaleConfig, res.getDisplayMetrics());
    return context;
}
 
Example 3
Source File: LocaleHelper.java    From candybar-library with Apache License 2.0 6 votes vote down vote up
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
 
Example 4
Source File: LocaleHelper.java    From wallpaperboard with Apache License 2.0 6 votes vote down vote up
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
 
Example 5
Source File: BaseActivity.java    From KernelAdiutor with GNU General Public License v3.0 6 votes vote down vote up
public static ContextWrapper wrap(Context context, Locale newLocale) {

        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

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

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

            context = context.createConfigurationContext(configuration);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = newLocale;
            res.updateConfiguration(configuration, res.getDisplayMetrics());
        }

        return new ContextWrapper(context);
    }
 
Example 6
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 7
Source File: LocaleUtils.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
private static void initLocale(Context c) {
    Crashlytics.setString("lang", Preferences.LANGUAGE.get());
    Crashlytics.setString("digits", Preferences.DIGITS.get());
    Configuration config = new Configuration();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = getLocales();
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
    } else {
        Locale locale = getLocale();
        config.setLocale(locale);
        Locale.setDefault(locale);
    }

    c.getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
    c.getApplicationContext().getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
}
 
Example 8
Source File: PictureLangUtils.java    From PicturePicker with Apache License 2.0 6 votes vote down vote up
public static Context setLanguage(Context context, Locale locale) {

        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        config.setLocale(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            return context.createConfigurationContext(config);
        } else {
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            context.getResources().updateConfiguration(config, displayMetrics);
            return context;
        }
    }
 
Example 9
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 10
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 11
Source File: ContextLocalWrapper.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  Configuration configuration = res.getConfiguration();

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

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

    context = context.createConfigurationContext(configuration);

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

  } else {
    configuration.locale = newLocale;
    res.updateConfiguration(configuration, res.getDisplayMetrics());
  }

  return new ContextLocalWrapper(context);
}
 
Example 12
Source File: ContextLocalWrapper.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  Configuration configuration = res.getConfiguration();

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

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

    context = context.createConfigurationContext(configuration);

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

  } else {
    configuration.locale = newLocale;
    res.updateConfiguration(configuration, res.getDisplayMetrics());
  }

  return new ContextLocalWrapper(context);
}
 
Example 13
Source File: LanguageUtil.java    From FimiX8-RE with MIT License 6 votes vote down vote up
@RequiresApi(api = 24)
public static void changeAppLanguage(Context context, Locale setLocale) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getLocaleByLanguage(setLocale);
    config.locale = locale;
    if (VERSION.SDK_INT >= 24) {
        LocaleList localeList = new LocaleList(new Locale[]{locale});
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
Example 14
Source File: Utils.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
private void updateAppConfig(final Activity activity) {
    Resources resources = Utils.getApp().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocales(activity.getResources().getConfiguration().getLocales());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(activity.getResources().getConfiguration().locale);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Utils.getApp().createConfigurationContext(config);
    } else {
        resources.updateConfiguration(config, dm);
    }
}
 
Example 15
Source File: LocaleManager.java    From ShizuruNotes with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = N)
private void setLocaleForApi24(Configuration config, Locale target) {
    Set<Locale> set = new LinkedHashSet<>();
    // bring the target locale to the front of the list
    set.add(target);

    LocaleList all = LocaleList.getDefault();
    for (int i = 0; i < all.size(); i++) {
        // append other locales supported by the user
        set.add(all.get(i));
    }

    Locale[] locales = set.toArray(new Locale[0]);
    config.setLocales(new LocaleList(locales));
}
 
Example 16
Source File: VMLanguage.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 7.x 以上修改语言方法
 */
@TargetApi(Build.VERSION_CODES.N)
public static Context updateLanguageResources(Context context, String language) {
    Resources res = context.getResources();
    Locale locale = getLocaleByLanguage(language);
    Configuration config = res.getConfiguration();
    config.setLocale(locale);
    config.setLocales(new LocaleList(locale));
    return context.createConfigurationContext(config);
}
 
Example 17
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 18
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 19
Source File: BaseActivity.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 20
Source File: LangHelper.java    From AppOpsX with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
  Resources resources = context.getResources();
  Locale locale = getLocaleByLanguage(context);

  Configuration configuration = resources.getConfiguration();
  configuration.setLocale(locale);
  configuration.setLocales(new LocaleList(locale));
  return context.createConfigurationContext(configuration);
}