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

The following examples show how to use android.content.res.Configuration#setLayoutDirection() . 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: LocaleHelper.java    From ncalc with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLayoutDirection(locale);
    }

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

    return context;
}
 
Example 2
Source File: LocaleManager.java    From firefox-echo-show with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This is public to allow for an activity to force the
 * current locale to be applied if necessary (e.g., when
 * a new activity launches).
 */
public void updateConfiguration(Context context, Locale locale) {
    Resources res = context.getResources();
    Configuration config = res.getConfiguration();

    // We should use setLocale, but it's unexpectedly missing
    // on real devices.
    config.locale = locale;

    config.setLayoutDirection(locale);

    res.updateConfiguration(config, null);
}
 
Example 3
Source File: IslamicLibraryApplication.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
private void updateLocale(@NonNull Context context, @NonNull Locale locale) {
    final Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLayoutDirection(config.locale);
    }
    resources.updateConfiguration(config, resources.getDisplayMetrics());
}
 
Example 4
Source File: LocaleHelper.java    From ncalc with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
    Locale.setDefault(locale);

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

    return context.createConfigurationContext(configuration);
}
 
Example 5
Source File: Support.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
public static void checkLanguage(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();

    config.locale = getLocal(context);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLayoutDirection(config.locale);
    }

    resources.updateConfiguration(config, dm);
}
 
Example 6
Source File: LocaleManager.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This is public to allow for an activity to force the
 * current locale to be applied if necessary (e.g., when
 * a new activity launches).
 */
public void updateConfiguration(Context context, Locale locale) {
    Resources res = context.getResources();
    Configuration config = res.getConfiguration();

    // We should use setLocale, but it's unexpectedly missing
    // on real devices.
    config.locale = locale;

    config.setLayoutDirection(locale);

    res.updateConfiguration(config, null);
}
 
Example 7
Source File: XposedMod.java    From XposedAppLocale with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private void setConfigurationLocale(Configuration config, Locale loc) {
    config.locale = loc;

    if (Build.VERSION.SDK_INT >= 17) {
        // Don't use setLocale() in order not to trigger userSetLocale
        config.setLayoutDirection(loc);
    }
}
 
Example 8
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 9
Source File: LocaleHelper.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    configuration.setLayoutDirection(locale);

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

    return context;
}
 
Example 10
Source File: LocalesUtils.java    From rosetta with MIT License 5 votes vote down vote up
/**
 *
 * @param context
 * @return true if the application locale changed
 */
static boolean setAppLocale(Context context, Locale newLocale)    {

    Resources resources = context.getResources();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    Configuration configuration = resources.getConfiguration();

    Locale oldLocale = new Locale(configuration.locale.getLanguage(), configuration.locale.getCountry());
    configuration.locale = newLocale;
    // Sets the layout direction from the Locale
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        sLogger.debug("Setting the layout direction");
        configuration.setLayoutDirection(newLocale);
    }
    resources.updateConfiguration(configuration, displayMetrics);

    if(oldLocale.equals(newLocale)) {
        return false;
    }

    if (LocalesUtils.updatePreferredLocale(newLocale))    {
        sLogger.info("Locale preferences updated to: " + newLocale);
        Locale.setDefault(newLocale);
    } else  {
        sLogger.error("Failed to update locale preferences.");
    }

    return true;
}
 
Example 11
Source File: DynamicLanguage.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
public static void setContextLocale(Context context, Locale selectedLocale) {
  Configuration configuration = context.getResources().getConfiguration();

  if (!configuration.locale.equals(selectedLocale)) {
    configuration.locale = selectedLocale;
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
      configuration.setLayoutDirection(selectedLocale);
    }
    context.getResources().updateConfiguration(configuration,
                                               context.getResources().getDisplayMetrics());
  }
}
 
Example 12
Source File: DynamicLanguage.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private static void setContextLocale(Context context, Locale selectedLocale) {
  Configuration configuration = context.getResources().getConfiguration();

  if (!configuration.locale.equals(selectedLocale)) {
    configuration.locale = selectedLocale;
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
      configuration.setLayoutDirection(selectedLocale);
    }
    context.getResources().updateConfiguration(configuration,
                                               context.getResources().getDisplayMetrics());
  }
}
 
Example 13
Source File: LocaleSettingHandler.java    From io.appium.settings with Apache License 2.0 5 votes vote down vote up
private void setLocaleWith(Locale locale) throws
        ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
    Class<?> activityManagerNativeClass = Class.forName("android.app.ActivityManagerNative");

    Method methodGetDefault = activityManagerNativeClass.getMethod("getDefault");
    methodGetDefault.setAccessible(true);
    Object amn = methodGetDefault.invoke(activityManagerNativeClass);

    // Build.VERSION_CODES.O
    if (Build.VERSION.SDK_INT >= 26) {
        // getConfiguration moved from ActivityManagerNative to ActivityManagerProxy
        activityManagerNativeClass = Class.forName(amn.getClass().getName());
    }

    Method methodGetConfiguration = activityManagerNativeClass.getMethod("getConfiguration");
    methodGetConfiguration.setAccessible(true);
    Configuration config = (Configuration) methodGetConfiguration.invoke(amn);

    Class<?> configClass = config.getClass();
    Field f = configClass.getField("userSetLocale");
    f.setBoolean(config, true);

    config.locale = locale;
    config.setLayoutDirection(locale);

    Method methodUpdateConfiguration = activityManagerNativeClass.getMethod("updateConfiguration", Configuration.class);
    methodUpdateConfiguration.setAccessible(true);
    methodUpdateConfiguration.invoke(amn, config);
}
 
Example 14
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);
}