SmallestScreenWidthDp for devices up to 3.2?

From reading the documentation to support multiple screen sizes, starting with Android 3.2, you can use smallestScreenWidthDpfor conditional typing of the layout, but is there anything for devices with pre-installed 3.2?

I have a fragment based layout and I would like to show both fragments on the screen if the screen size is more than 600dp.

This is the code that I use to install the fragments that I would like to find:

public class MyActivity extends FragmentActivity  
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
            finish();
            return;
        }

        if (savedInstanceState == null) {
            final DetailFragment details = new DetailFragment();
            details.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}
+3
source share
2 answers

, "SmallestScreenWidthDp" (http://developer.android.com/guide/practices/screens_support.html) - 3.2 . pre 3.2 Configuration, .

: ( ) ...

0

:

public static int getSmallestScreenWidthDp(Context context) {
    Resources resources = context.getResources();
    try {
        Field field = Configuration.class.getDeclaredField("smallestScreenWidthDp");
        return (Integer) field.get(resources.getConfiguration());
    } catch (Exception e) {
        // not perfect because reported screen size might not include status and button bars
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        int smallestScreenWidthPixels = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
        return Math.round(smallestScreenWidthPixels / displayMetrics.density);
    }
}

, , DisplayMetrics .

, Galaxy 10.1 800, 752.

+2

All Articles