Question

I would like to do the following in my app:

  • Portrait mode only for the phone
  • Portrait mode and landscape mode for tablets.

I know i can easily change the orientation in the manifest file but that will affect the entire application.

I have also thought of creating separate activities, to handle the different versions but i don't know how to detect the type of device using the application.

Does anyone know how to tackle this?

Was it helpful?

Solution

You can use the following piece of code to differentiate between normal/large screen and block the orientation change accordingly.

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

OTHER TIPS

In activity's onCreate method check whether app is running on phone or on tablet. If app is running on phone set activity screen orientation to portrait only.

Add these files to your res folder.

res/values/vars.xml:

<?xml version="1.0" encoding="utf-8"?><resources><bool name="is_tablet">false</bool></resources>



res/values-sw600dp/vars.xml:

<?xml version="1.0" encoding="utf-8"?><resources><bool name="is_tablet">true</bool></resources>

In onCreate method off all your activites add this code:

    if (!getResources().getBoolean(R.bool.is_tablet)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

You have to identify the tablet with Android screen qualifiers: Screen qualifiers

Then you put in the right layout folder the settings you want. For example you can add the attribute orientation:portrait|landscape only in the layout xml file for large screens, and orientation:portrait for all the others. See the folder structures here:

res/layout/my_layout.xml // layout for normal screen size ("default") res/layout-small/my_layout.xml // layout for small screen size res/layout-large/my_layout.xml // layout for large screen size res/layout-xlarge/my_layout.xml // layout for extra large screen size res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

Take a look to the google guide: http://developer.android.com/guide/practices/screens_support.html

i suppose you can do small trick for it, try use Configuration.screenLayout :

  switch (this.getResources().getConfiguration().screenLayout) {
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
                return "small";
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                return "normal";
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                return "large";
            case 4: // screen xlarge 
                return "xlarge";
            default:
                return "undefined";
            }
        }

thanks

The problem that you are coming across unfortunately becomes a grey area. We can decipher what density a phone is and the physical size of a device. But there is nothing that really decides to us, what a phone is and what a tablet it. With phones being sometimes 7 inches tall and some tablets dedicated as just e-readers and such, they really could be any density or size.

However, the table suggested by Santacrab above could be used along with using the appropriate layout attributes that will resize accordingly regardless of the size of the screen. There will be circumstances of course where it makes sense to use fragments to split screens and on a smaller screen to avoid overcrowding the screen, but the links that spin off of the link from Santacrab should provide some good guidance on that as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top