Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Determine whether a multi-fragment layout should be used

When making apps for Android 3.x, you could basically just check if the device was in landscape to see if it could fit multiple fragments side by side. But this is only because you were basically guaranteed that the device is a tablet.

With the introduction of the compatibility library and android 4.0, I can't think of what I would consider as a 'good' way to determine if I can get away with side by side fragments or should revert to standard one fragment per 'screen.'

In the NewsReader example app, Google has a special values file for every common resolution, each with a Boolean value for whether the resolution should support the 2 fragment layout, but I think this way is poorly conceived. The only way I can think of is to check the size of the screen (to guess if it is a tablet or at least big enough to not ruin the layout), and then check the orientation.

So if anyone out there has an idea how to easily and efficiently check this, please let me know!

like image 479
Jessy Diamond Exum Avatar asked Dec 27 '11 02:12

Jessy Diamond Exum


1 Answers

You can actually see the screen size type (small, normal, large, etc) by using the following:

getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK

That'll return an int that you can check against any of the following:

Configuration.SCREENLAYOUT_SIZE_SMALL
Configuration.SCREENLAYOUT_SIZE_NORMAL
Configuration.SCREENLAYOUT_SIZE_LARGE
Configuration.SCREENLAYOUT_SIZE_UNDEFINED

Then you can check for orientation, using

getResources().getConfiguration().orientation

Which will match any of the following possibilities

Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_LANDSCAPE
Configuration.ORIENTATION_SQUARE
Configuration.ORIENTATION_UNDEFINED

With this you can essentially simulate the multiple views by adding your fragments as space allows based on screen size and orientation.

like image 188
Troy McCabe Avatar answered Nov 14 '22 22:11

Troy McCabe