GetCurrentSizeRange () on Android before 4.1

In Android API 16 (4.1 Jelly Bean), and later we have a getCurrentSizeRange method to get the width and height range.How to get the size range in versions before 4.1?

I tried to study the source code to see how the range is calculated. This is done differently on different Android versions, and I couldn't find any logic that calculates these dimensions. Any pointers that can help me find this are greatly appreciated.

+3


source to share


1 answer


If you're just trying to figure out heights and widths for portrait and landscape orientations, you can simply speed up the orientation in both directions and get a metric in each direction. The next snippet does this. The code is protected by a simple preference check, since I checked this in onCreate()

, and orientation changes will cause the activity to restart (and go into a loop). In your application, you probably want to do something more specific. Additionally, all methods used are valid up to API 1, but can be replaced or renamed to higher versions.



SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("firstTime", true)) {
    prefs.edit().putBoolean("firstTime", false).commit();

    DisplayMetrics dm = new DisplayMetrics();

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    Log.e(TAG, String.format("landscape is %d x %d",  dm.widthPixels, dm.heightPixels));
    // Do something with the values, perhaps saving them in prefs

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    Log.e(TAG, String.format("portrait is %d x %d",  dm.widthPixels, dm.heightPixels));
    // Do something with the values, perhaps saving them in prefs
}

      

+1


source







All Articles