Example usage for android.content Context createConfigurationContext

List of usage examples for android.content Context createConfigurationContext

Introduction

In this page you can find the example usage for android.content Context createConfigurationContext.

Prototype

public abstract Context createConfigurationContext(@NonNull Configuration overrideConfiguration);

Source Link

Document

Return a new Context object for the current Context but whose resources are adjusted to match the given Configuration.

Usage

From source file:Main.java

/**
 * Setting {@link Locale} for {@link Context}.
 *//*  www .j a v  a2 s  .c  o m*/
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:im.vector.VectorApp.java

/**
 * Get String from a locale//from   ww  w.  ja v  a 2s.  c  o m
 *
 * @param context    the context
 * @param locale     the locale
 * @param resourceId the string resource id
 * @return the localized string
 */
private static String getString(Context context, Locale locale, int resourceId) {
    String result;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Configuration config = new Configuration(context.getResources().getConfiguration());
        config.setLocale(locale);
        try {
            result = context.createConfigurationContext(config).getText(resourceId).toString();
        } catch (Exception e) {
            Log.e(LOG_TAG, "## getString() failed : " + e.getMessage());
            // use the default one
            result = context.getString(resourceId);
        }
    } else {
        Resources resources = context.getResources();
        Configuration conf = resources.getConfiguration();
        Locale savedLocale = conf.locale;
        conf.locale = locale;
        resources.updateConfiguration(conf, null);

        // retrieve resources from desired locale
        result = resources.getString(resourceId);

        // restore original locale
        conf.locale = savedLocale;
        resources.updateConfiguration(conf, null);
    }

    return result;
}

From source file:im.vector.VectorApp.java

/**
 * Compute a localised context//from w  w  w. ja v  a 2s .c  o 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:com.android.settings.PreviewPagerAdapter.java

public PreviewPagerAdapter(Context context, boolean isLayoutRtl, int[] previewSampleResIds,
        Configuration[] configurations) {
    mIsLayoutRtl = isLayoutRtl;/*from ww w .j  av a  2s.com*/
    mPreviewFrames = new FrameLayout[previewSampleResIds.length];
    mViewStubInflated = new boolean[previewSampleResIds.length][configurations.length];

    for (int i = 0; i < previewSampleResIds.length; ++i) {
        int p = mIsLayoutRtl ? previewSampleResIds.length - 1 - i : i;
        mPreviewFrames[p] = new FrameLayout(context);
        mPreviewFrames[p].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

        for (int j = 0; j < configurations.length; ++j) {
            // Create a new configuration for the specified value. It won't
            // have any theme set, so manually apply the current theme.
            final Context configContext = context.createConfigurationContext(configurations[j]);
            configContext.setTheme(context.getThemeResId());

            final LayoutInflater configInflater = LayoutInflater.from(configContext);
            final ViewStub sampleViewStub = new ViewStub(configContext);
            sampleViewStub.setLayoutResource(previewSampleResIds[i]);
            final int fi = i, fj = j;
            sampleViewStub.setOnInflateListener(new OnInflateListener() {
                @Override
                public void onInflate(ViewStub stub, View inflated) {
                    inflated.setVisibility(stub.getVisibility());
                    mViewStubInflated[fi][fj] = true;
                }
            });

            mPreviewFrames[p].addView(sampleViewStub);
        }
    }
}