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:com.ryg.dynamicload.DLProxyFragmentActivity.java

protected void loadResources() {
    try {// www.j  a  v  a  2 s  .c  om
        AssetManager assetManager = AssetManager.class.newInstance();
        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
        addAssetPath.invoke(assetManager, mDexPath);
        mAssetManager = assetManager;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Resources superRes = super.getResources();
    mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
}

From source file:org.mariotaku.twidere.activity.support.DualPaneActivity.java

@Override
protected void onCreate(final Bundle savedInstanceState) {
    mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    super.onCreate(savedInstanceState);
    final Resources res = getResources();
    final int orientation = res.getConfiguration().orientation;
    final int layout;
    final boolean is_large_screen = res.getBoolean(R.bool.is_large_screen);
    mDualPaneInPortrait = mPreferences.getBoolean(PREFERENCE_KEY_DUAL_PANE_IN_PORTRAIT, is_large_screen);
    mDualPaneInLandscape = mPreferences.getBoolean(PREFERENCE_KEY_DUAL_PANE_IN_LANDSCAPE, is_large_screen);
    switch (orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        layout = mDualPaneInLandscape || shouldForceEnableDualPaneMode() ? getDualPaneLayoutRes()
                : getNormalLayoutRes();//from   w ww  .  j  av a2s  . c  o m
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        layout = mDualPaneInPortrait || shouldForceEnableDualPaneMode() ? getDualPaneLayoutRes()
                : getNormalLayoutRes();
        break;
    default:
        layout = getNormalLayoutRes();
        break;
    }
    setContentView(layout);
    if (mSlidingPane != null) {
        mSlidingPane.setRightPaneBackground(getPaneBackground());
    }
    final FragmentManager fm = getSupportFragmentManager();
    fm.addOnBackStackChangedListener(this);
    if (savedInstanceState != null) {
        updateMainViewVisibility();
    }
}

From source file:com.android.projectz.teamrocket.thebusapp.settings.SettingLanguageActivity.java

public void setLocale(String lang) {
    Resources res = this.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang.toLowerCase());
    res.updateConfiguration(conf, dm);//from   w w w . j a  va2s . c  o m
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    /*
    FLAG_ACTIVITY_CLEAR_TOP
    If set, and the activity being launched is already running in the current task,
    then instead of launching a new instance of that activity, all of the other
    activities on top of it will be closed and this Intent will be delivered to
    the (now on top) old activity as a new Intent.
     */
    startActivity(intent);
}

From source file:net.eledge.android.europeana.gui.activity.HomeActivity.java

public void setLocale(String lang) {
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang);
    res.updateConfiguration(conf, dm);//  w  w w.  j ava2 s  . c  o m
    Intent refresh = new Intent(this, HomeActivity.class);
    startActivity(refresh);
}

From source file:im.vector.VectorApp.java

/**
 * Get String from a locale//  ww  w .  ja v a 2s  . c om
 *
 * @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

/**
 * Provides the device locale/*from w w w  .j  a v a2 s  .c  o  m*/
 *
 * @return the device locale
 */
public static Locale getDeviceLocale() {
    Context context = VectorApp.getInstance();
    Locale locale = getApplicationLocale();

    try {
        PackageManager packageManager = context.getPackageManager();
        Resources resources = packageManager.getResourcesForApplication("android");
        locale = resources.getConfiguration().locale;
    } catch (Exception e) {
        Log.e(LOG_TAG, "## getDeviceLocale() failed " + e.getMessage());
    }

    return locale;
}

From source file:ubc.cs.cpsc210.sustainabilityapp.UBCSustainabilityAppActivity.java

/**
 * Determine tag for first tab to be displayed - restore from previous instance, if available.
 * /*  w w w .  j  a v  a  2 s . c o  m*/
 * @param savedInstanceState  the saved instance state
 * @param res   the application's resources
 * @return  the tag of the tab to be displayed when activity is (re)created.
 */
private String findInitialTabTag(Bundle savedInstanceState, Resources res) {
    if (savedInstanceState != null) {
        if (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            initialTabTag = savedInstanceState.getString("tab");
        else if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
                && !savedInstanceState.getString("tab").equals(MAP))
            initialTabTag = savedInstanceState.getString("tab");
        else
            // we are in landscape mode and saved tab tag is "map", so need to switch to another tab 
            // (let's arbitrarily choose POI).
            initialTabTag = "poi";
    } else
        initialTabTag = "poi";

    return initialTabTag;
}

From source file:com.tmall.wireless.tangram.view.LinearScrollView.java

private int getScreenWidth() {
    Resources resources = getContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration configuration = resources.getConfiguration();
    return configuration.orientation == Configuration.ORIENTATION_PORTRAIT ? dm.widthPixels : dm.heightPixels;
}

From source file:de.theknut.xposedgelsettings.ui.MainActivity.java

@SuppressWarnings("deprecation")
@Override//from  www . ja  v a2 s  .  co  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContext = CommonUI.CONTEXT = mActivity = CommonUI.ACTIVITY = this;

    if (getSharedPreferences(Common.PREFERENCES_NAME, Context.MODE_WORLD_READABLE)
            .getBoolean("forceenglishlocale", false)) {
        Resources res = mContext.getResources();
        Configuration conf = res.getConfiguration();
        conf.locale = new Locale(Locale.US.getDisplayLanguage().toLowerCase());
        res.updateConfiguration(conf, res.getDisplayMetrics());
    }

    mTitle = mDrawerTitle = getTitle();
    mFragmentTitles = getResources().getStringArray(R.array.fragmenttitles_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.getRootView().setBackgroundColor(CommonUI.UIColor);
    getActionBar().setBackgroundDrawable(new ColorDrawable(CommonUI.UIColor));

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mFragmentTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    mDrawerLayout.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getRepeatCount() != 0)
                return false;

            if (!mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {

                if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU)) {
                    mDrawerLayout.openDrawer(Gravity.LEFT);
                    return true;
                }
            } else {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    MainActivity.this.finish();
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_MENU) {
                    mDrawerLayout.closeDrawer(Gravity.LEFT);
                    return true;
                }
            }

            return false;
        }
    });

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setBackgroundDrawable(new ColorDrawable(CommonUI.UIColor));

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    SharedPreferences prefs = getSharedPreferences(Common.PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
    CommonUI.NO_BACKGROUND_IMAGE = prefs.getBoolean("nobackgroundimage", false);
    CommonUI.AUTO_BLUR_IMAGE = prefs.getBoolean("autoblurimage", false);

    if (savedInstanceState == null) {

        Intent i = getIntent();
        if (i != null && i.hasExtra("fragment")) {
            if (i.getStringExtra("fragment").equals("badges")) {
                selectItem(6);
            }
        } else {
            selectItem(0);
        }
    }
}

From source file:im.vector.VectorApp.java

/**
 * Compute a localised context/*w ww  . j  a v a  2  s.  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;
}