Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting different orientations for Phone and Tablet on Android

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?

like image 442
AndroidEnthusiast Avatar asked Dec 05 '22 09:12

AndroidEnthusiast


2 Answers

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;
}
like image 87
silvermouse Avatar answered Jan 20 '23 06:01

silvermouse


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);
    }
like image 41
Egis Avatar answered Jan 20 '23 04:01

Egis