Example usage for android.content.res Resources getConfiguration

List of usage examples for android.content.res Resources getConfiguration

Introduction

In this page you can find the example usage for android.content.res Resources getConfiguration.

Prototype

public Configuration getConfiguration() 

Source Link

Document

Return the current configuration that is in effect for this resource object.

Usage

From source file:Main.java

public static void setLanguage(Context context, String language) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (mLocaleMap == null) {
        config.locale = mDefaultLocale;/*from ww w.j av a  2s .  c o m*/
    } else {
        if (mLocaleMap.containsKey(language)) {
            config.locale = mLocaleMap.get(language);
        } else {
            config.locale = mDefaultLocale;
        }
    }
    resources.updateConfiguration(config, dm);
}

From source file:Main.java

public static void updateConfiguration(Context context) {
    Resources resources = context.getResources();
    Configuration conf = resources.getConfiguration();
    Locale current = Locale.getDefault();
    String currentLanguage = current.getDisplayLanguage();

    if (Locale.CHINESE.getDisplayLanguage().equals(currentLanguage)
            || Locale.ENGLISH.getDisplayLanguage().equals(currentLanguage)) {
        return;//  w  w w  . jav a  2 s  . com
    }

    conf.locale = Locale.ENGLISH;
    resources.updateConfiguration(conf, resources.getDisplayMetrics());
}

From source file:Main.java

/**
 * Sets the system locale for this process.
 *
 * @param res the resources to use. Pass current resources.
 * @param newLocale the locale to change to.
 * @return the old locale.//  ww  w  . j a va 2  s. c  o  m
 */
public static Locale setSystemLocale(final Resources res, final Locale newLocale) {
    final Configuration conf = res.getConfiguration();
    final Locale saveLocale = conf.locale;
    conf.locale = newLocale;
    res.updateConfiguration(conf, res.getDisplayMetrics());
    return saveLocale;
}

From source file:Main.java

/**
 * Get a dimension for the specified orientation.
 * This method may be heavy since it updates the {@code resources} twice.
 *//*from  w w w  . ja  v a2  s  . com*/
public static float getDimensionForOrientation(Resources resources, int id, int orientation) {
    Configuration configuration = resources.getConfiguration();
    if (configuration.orientation == orientation) {
        return resources.getDimension(id);
    }

    Configuration originalConfiguration = new Configuration(resources.getConfiguration());
    try {
        configuration.orientation = orientation;
        resources.updateConfiguration(configuration, null);
        return resources.getDimension(id);
    } finally {
        resources.updateConfiguration(originalConfiguration, null);
    }
}

From source file:Main.java

/**
 * Setting {@link Locale} for {@link Context}.
 *//*from   ww  w. jav a2s .c om*/
public static Context applyLanguageForContext(Context context, 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;
    }
}

From source file:Main.java

public static void setLocale(Context paramContext, Locale paramLocale) {
    Resources localResources = paramContext.getResources();
    Configuration localConfiguration = localResources.getConfiguration();
    if (localConfiguration.locale.equals(paramLocale)) {

    } else {/*from   w  ww .j  ava  2  s.  c o  m*/
        DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
        localConfiguration.locale = paramLocale;
        localResources.updateConfiguration(localConfiguration, localDisplayMetrics);
        Resources.getSystem().updateConfiguration(localConfiguration, localDisplayMetrics);
    }
}

From source file:Main.java

public static Resources createResources(Context context, AssetManager assetManager) {
    Resources superRes = context.getResources();
    return new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
}

From source file:Main.java

private static void updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);//  w w  w.  ja  va2 s .co m

    Resources resources = context.getResources();

    Configuration configuration = new Configuration(resources.getConfiguration());

    configuration.locale = locale;

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

From source file:Main.java

/**
 * @return a localized version of the text resource specified by resId
 *//*from ww w .  ja v  a2 s.co m*/
static CharSequence getTextForLocale(Context context, Locale locale, int resId) {
    final Resources res = context.getResources();
    final Configuration config = res.getConfiguration();
    final Locale prevLocale = config.locale;
    try {
        config.locale = locale;
        res.updateConfiguration(config, null);
        return res.getText(resId);
    } finally {
        config.locale = prevLocale;
        res.updateConfiguration(config, null);
    }
}

From source file:Main.java

public static int getNavigationBarHeight(Context c) {
    int result = 0;
    boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

    if (!hasMenuKey && !hasBackKey) {
        //The device has a navigation bar
        Resources resources = c.getResources();

        int orientation = resources.getConfiguration().orientation;
        int resourceId;
        if (isTablet(c)) {
            resourceId = resources//from w ww.ja v  a  2  s  .  c  o m
                    .getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height"
                            : "navigation_bar_height_landscape", "dimen", "android");
        } else {
            resourceId = resources
                    .getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height"
                            : "navigation_bar_width", "dimen", "android");
        }

        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
    }
    return result;
}