Example usage for android.content.res Configuration setLayoutDirection

List of usage examples for android.content.res Configuration setLayoutDirection

Introduction

In this page you can find the example usage for android.content.res Configuration setLayoutDirection.

Prototype

public void setLayoutDirection(Locale loc) 

Source Link

Document

Set the layout direction from a Locale.

Usage

From source file:im.vector.VectorApp.java

/**
 * Compute a localised context//from w  w w  .j  a  v  a  2  s  .  co m
 *
 * @param context the context
 * @return the localised context
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static Context getLocalisedContext(Context context) {
    try {
        Resources resources = context.getResources();
        Locale locale = getApplicationLocale();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = getFontScaleValue();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale);
            configuration.setLayoutDirection(locale);
            return context.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLayoutDirection(locale);
            }
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
            return context;
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "## getLocalisedContext() failed : " + e.getMessage());
    }

    return context;
}

From source file:org.akvo.caddisfly.ui.activity.MainActivity.java

/**
 * Load user preferences/* www.  j  a  v  a 2s .com*/
 */
private void loadSavedPreferences() {
    assert getApplicationContext() != null;

    // Set the locale according to preference
    Locale myLocale = new Locale(PreferencesUtils.getString(this, R.string.languageKey, Config.DEFAULT_LOCALE));
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLayoutDirection(myLocale);
    }
    res.updateConfiguration(conf, dm);
}