Kenny Wyland
Kenny Wyland

Reputation: 21880

Android: allow portrait and landscape for tablets, but force portrait on phone?

I would like tablets to be able to display in portrait and landscape (sw600dp or greater), but phones to be restricted to portrait only. I can't find any way to conditionally choose an orientation. Any suggestions?

Upvotes: 227

Views: 109236

Answers (15)

Syed Taha
Syed Taha

Reputation: 1

If you want to force portrait on phones and allow landscape in tablet, do the following:

Step 1: Set android:screenOrientation="locked" to the Activity in AndroidManifest.xml

Step 2: Create a tablet Qualifer.

Step 3:

val configuration = resources.configuration
val smallestScreenWidthDp = configuration.smallestScreenWidthDp

if (smallestScreenWidthDp>=600){
    //   for tablet 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
    //   for phone   
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Upvotes: 0

tiwiz
tiwiz

Reputation: 327

Suggested approach would be to not lock orientation for any window dimension, but you can achieve it by following the guide here.

In steps, first thing to do would be to unlock orientation on the manifest

<activity
    android:name=".MyActivity"
    android:screenOrientation="fullUser">

Next, you can use Jetpack Window Manager to determine how much space the app has on screen:

/** Determines whether the device has a compact screen. **/
fun compactScreen(): Boolean {
    val screenMetrics = WindowMetricsCalculator
                        .getOrCreate()
                        .computeMaximumWindowMetrics(this)
    val shortSide = min(screenMetrics.bounds.width(),
                        screenMetrics.bounds.height())
    return shortSide / resources.displayMetrics.density < 600
}

and finally you lock the orientation when the space is smaller, usually on phones:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestedOrientation = if (compactScreen())
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else
        ActivityInfo.SCREEN_ORIENTATION_FULL_USER
    ...
}

Upvotes: 0

TwiXter
TwiXter

Reputation: 300

From developer.android.com you should declare your screen Orientation in your manifest with value 'nosensor':

The orientation is determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device.

The rotation will then be the 'natural' device behaviour (For phones: Portrait / for Tablets: landscape).

Advandage is that you can mix up different rotations per activity, for example, if you have an activity showing database records, then you can set this activity orientation to "fullUser" while others are using "nosensor". "fullUser" Activity will rotate depending user selection (rotation enabled: uses sensor otherwhise uses user preference).

<activity android:name=".scanner.ScannerPropertiesActivity" 
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_scanner_properties"
android:screenOrientation="${screenOrientation}"/>

${screenOrientation} is a ManifestPlaceHolder so i could switch easily all the Activities i wish to test between all orientation behaviours using this:

//module's build.gradle
defaultConfig {
//...
    manifestPlaceholders = [applicationName:appName,screenOrientation:'nosensor']
//...
}

If you need something more complex (ie: Allow Screen rotation only in a tablet and fix phone orientation to portrait) then you should use locked:

android:screenOrientation="locked" I have noticed that if you force orientation to portrait while sensor sends that phone is rotated in landscape, your Activity will start in landscape and then rotate to portrait once your code is executed, with almost any other orientation. lock keeps the previous rotation, so you may be already locked in portrait from your previous activity. Then you must know when a device is a tablet or a phone: To do so i recommend you to use this:

Detect tablet using resource overload

//normal tablet_detection file content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="isTablet" type="bool">false</item>
</resources>
//sw600dp tablet_detection file content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="isTablet" type="bool">true</item>
</resources>
//Now in your code you can detect if it's a tablet or not by calling following function in your Activity's **onCreate** and apply screen orientation by code:
protected  void updateScreenOrientation(){
boolean isTablet =context.getResources().getBoolean(R.bool.isTablet);
//And select the orientation you wish to set:
int defaultOrientation=isTablet? ActivityInfo.SCREEN_ORIENTATION_FULL_USER:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        if(getRequestedOrientation()== ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
            //Apply this only on Activities where you set the orientation to 'locked' in the manifest
            this.setRequestedOrientation(defaultOrientation);
        }
}

Upvotes: 1

ikbalkazanc
ikbalkazanc

Reputation: 171

I added this code part inside toMainActivity.java file. Its worked for my case.

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            handleOrientationConfiguration();
        }

        private void handleOrientationConfiguration() {

            if (isTablet()) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }

        private boolean isTablet() {
            Configuration configuration = this.getContext().getResources()
                    .getConfiguration();/*from   www .j ava  2  s. c  o  m*/
            return (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
        }

Upvotes: 0

Micer
Micer

Reputation: 8979

I had issues with provided solutions, finally this is what worked for me:

  1. In AndroidManifest.xml set Activity's orientation to "locked":
<activity
    android:name="com.whatever.YourActivity"
    android:screenOrientation="locked"
    ... />
  1. In OnCreate(...) method in your Activity, use the following:
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
    if (isTablet(resources)) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    } else if (resources.configuration.orientation != Configuration.ORIENTATION_PORTRAIT) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }
    super.onCreate(savedInstanceState)
}

Note that you don't need to use hacky solutions with boolean values in resources, you can use isTablet(resources) method from DeviceProperties class, which apart from other things checks if smallestScreenWidthDp >= 600.

Upvotes: 8

Soumen Das
Soumen Das

Reputation: 1302

In the manifest file you add following code:

android:screenOrientation="fullSensor"

and XML file create under layout-land folder

Upvotes: 0

Stuart Campbell
Stuart Campbell

Reputation: 1147

I recently came across this requirement but didn't want to use any of these solutions. All of them have in common that they call setRequestedOrientation programmatically. The problem with this is it sets the orientation too late and causes slight UI glitches. If your device is in landscape when you launch the app it loads in landscape then rotates. This is particularly noticeable if you use a theme to create a splash screen effect as you will see the background image in the wrong orientation. This can also have a side effect on how your app is shown in the recent apps and issues with configuration change as noted in other answers comments.

Conclusion the correct orientation NEEDS to be set in the manifest so the system knows the orientation without launching your app.

Here is a solution on how to do this (it is quite a bit of effort, but you will sleep better)

First set the orientation in the manifest as a placeholder

<activity
      android:screenOrientation="${screenOrientation}"
     ...
</activity>

We then need to add a normal/tablet flavour to set the value in the app/build.gradle. (Could be achieved with a new build type if your already using flavours)

android {
    ...
    productFlavors {
        normal {
            manifestPlaceholders = [screenOrientation: "portrait"]
        }
        tablet {
            manifestPlaceholders = [screenOrientation: "unspecified"]
        }
    }
}

Now we need to tell the tablet build that it is just for large devices. This can be done by adding a tablet only manifest to merge with the default one. Add a new manifest file at->

app
 src
  tablet
   AndroidManifest.xml

Below is all this manifest needs, because it is merged with the default one is ->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="{package}">

    <supports-screens
        android:smallScreens="false"
        android:normalScreens="false"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:requiresSmallestWidthDp="600"
        />

</manifest>

The final trick is we need to differentiate builds by version code because the Playstore can not have two uploads with matching codes. We want to make sure the tablet (the more restrictive build) has the higher code. An easy way is to take a base code, then for tablet base * 2 and normal base * 2 - 1. I use CI for this with base code being build number but easy enough to hard code in your flavours.

Now build both flavours to create

 app-normal.apk/aab (v1.0.0, version code 1)
 app-tablet.apk/aab (v1.0.0, version code 2)

Upload them to Playstore as multiple apk/aab uploads then if downloaded on a tablet the Playstore serves them the apk that rotates and if downloaded on a phone the portrait only one.

Note: only works if distributing via the Google Playstore/Amazon Kindle

Further reading https://developer.android.com/google/play/publishing/multiple-apks https://developer.amazon.com/docs/fire-tablets/ft-screen-layout-and-resolution.html

Upvotes: 5

Rahul
Rahul

Reputation: 3349

Following accepted answer, I am adding the kotlin file with the solution hope it helps someone

Put this bool resource in res/values as bools.xml or whatever (file names don't matter here):

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

Put this one in res/values-sw600dp and res/values-sw600dp-land:

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

Then, add it in below lines in your activity or fragmentvity

class MyActivity : Activity() {

    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        if (resources.getBoolean(R.bool.portrait_only)) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onCreate(savedInstanceState)
    }

    @SuppressLint("SourceLockedOrientationActivity")
    override fun onConfigurationChanged(newConfig: Configuration) {
        if (resources.getBoolean(R.bool.portrait_only)) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onConfigurationChanged(newConfig)
    }
}

Upvotes: 4

mengoni
mengoni

Reputation: 258

Other solutions didn't work for me. I still had some weird orientation problem with dialogs and recreation issues. My solution was to extend the Activity, forcing it as portrait in manifest.

Example:

public class MainActivityPhone extends MainActivity {}

manifest.xml:

        <activity
        android:screenOrientation="portrait"
        android:name=".MainActivityPhone"
        android:theme="@style/AppTheme.NoActionBar" />

in splashcreen activity:

    Intent i = null;
    boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
    if (!isTablet)
        i = new Intent(this, MainActivityPhone.class);
    else
        i = new Intent(this, MainActivity.class);
    startActivity(i);

Upvotes: 1

mathew11
mathew11

Reputation: 3590

Unfortunately, using the method setRequestedOrientation(...) will cause the activity to restart, so even if you call this in the onCreate method it will go through the activity lifecycle and then it will recreate the same activity in the requested orientation. So at @Brian Christensen's answer you should consider that the activity code might be called twice, this could have bad effects (not only visual, but also at network requests, analytics, etc.).

Furthermore, to set the configChanges attribute in the manifest is in my opinion a big trade-off, which could take massive refactoring cost. Android Devs are not recommending to change that attribute.

Finally, trying to set the screenOrientation somehow different (to avoid the restarting problem) is impossible, statically impossible due to the static manifest which can't be changed, programmatically it is only possible to call that method in the already started activity.

Summary: In my opinion, @Brian Christensen suggestion is the best trade-off, but be aware of the restarting activity issue.

Upvotes: 0

RominaV
RominaV

Reputation: 3425

Following Ginny's answer, I think the most reliable way to do it is as follows:

As described here, put a boolean in resources sw600dp. It must have the prefix sw otherwise it won't work properly:

in res/values-sw600dp/dimens.xml

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

in res/values/dimens.xml

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

Then make a method to retrieve that boolean:

public class ViewUtils {
    public static boolean isTablet(Context context){
        return context.getResources().getBoolean(R.bool.isTablet);
    }
}

And a base activity to extend from the activities where you want this behaviour:

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!ViewUtils.isTablet(this)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}

So each activity would extend BaseActivity:

public class LoginActivity extends BaseActivity //....

Important: even if you extend from BaseActivity, you must add the line android:configChanges="orientation|screenSize" to each Activity in your AndroidManifest.xml:

    <activity
        android:name=".login.LoginActivity"
        android:configChanges="orientation|screenSize">
    </activity>

Upvotes: 11

Suragch
Suragch

Reputation: 512666

Supplement to the accepted answer

You can do the following steps in Android Studio to add the res/values-sw600dp and res/values-large directories with their bools.xml files.

values-sw600dp

First of all, from the Project tab select the Project (rather than Android) filter in the navigator.

enter image description here

Then right click the app/src/main/res directory. Choose New > Android Resource Directory.

Select Smallest Screen Width, and then press the >> button.

enter image description here

Type in 600 for the smallest screen width. The directory name will be automatically generated. Say OK.

enter image description here

Then right click on the newly created values-sw600dp file. Choose New > Values resource file. Type bools for the name.

values-large

Adding a values-large directory is only necessary if you are supporting pre Android 3.2 (API level 13). Otherwise you can skip this step. The values-large directory corresponds to values-sw600dp. (values-xlarge corresponds to values-sw720dp.)

To create the values-large directory, follow the same steps as above, but in this case choose Size rather than Smallest Screen Width. Select Large. The directory name will be automatically generated.

enter image description here

Right click the directory as before to create the bools.xml file.

Upvotes: 23

Brian Christensen
Brian Christensen

Reputation: 5136

Here's a good way using resources and size qualifiers.

Put this bool resource in res/values as bools.xml or whatever (file names don't matter here):

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

Put this one in res/values-sw600dp and res/values-xlarge:

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

See this supplemental answer for help adding these directories and files in Android Studio.

Then, in the onCreate method of your Activities you can do this:

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

Devices that are more than 600 dp in the smallest width direction, or x-large on pre-Android 3.2 devices (tablets, basically) will behave like normal, based on sensor and user-locked rotation, etc. Everything else (phones, pretty much) will be portrait only.

Upvotes: 489

Codebeat
Codebeat

Reputation: 6610

Old question I know. In order to run your app always in portrait mode even when orientation may be or is swapped etc (for example on tablets) I designed this function that is used to set the device in the right orientation without the need to know how the portrait and landscape features are organised on the device.

   private void initActivityScreenOrientPortrait()
    {
        // Avoid screen rotations (use the manifests android:screenOrientation setting)
        // Set this to nosensor or potrait

        // Set window fullscreen
        this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DisplayMetrics metrics = new DisplayMetrics();
        this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

         // Test if it is VISUAL in portrait mode by simply checking it's size
        boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); 

        if( !bIsVisualPortrait )
        { 
            // Swap the orientation to match the VISUAL portrait mode
            if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
             { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
            else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
        }
        else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }

    }

Works like a charm!

NOTICE: Change this.activity by your activity or add it to the main activity and remove this.activity ;-)

If you want to do the opposite, you must change the code to landscape (but I think it is clear how to this).

Upvotes: 0

Ginny
Ginny

Reputation: 3121

Here's how I did it (inspired by http://androidblogger.blogspot.com/2011/08/orientation-for-both-phones-and-tablets.html ):

In AndroidManifest.xml , for each activity you want to be able to change between portrait and landscape (make sure you add screenSize - you didn't used to need this!) You don't need to set a screen orientation here. :

android:configChanges="keyboardHidden|orientation|screenSize"

Methods to add in each Activity:

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

and: (if you don't override this method, the app will call onCreate() when changing orientations)

@Override
public void onConfigurationChanged (Configuration newConfig)
{       
    super.onConfigurationChanged(newConfig);

    if (!isXLargeScreen(getApplicationContext()) ) {            
        return; //keep in portrait mode if a phone      
    }

    //I set background images for landscape and portrait here
}

In onCreate() of each Activity :

if (!isXLargeScreen(getApplicationContext())) { //set phones to portrait; 
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);            
}
else {
  //I set background images here depending on portrait or landscape orientation 
}

The only thing I can't seem to figure out is how to to get the app to change layout files when switching from landscape to portrait or vice versa. I assume the answer is doing something similar to what the above link does, but I couldn't get that to work for me - it deleted all my data. But if you have a simple enough app that you have the same layout file for portrait and landscape, this should work.

Upvotes: 9

Related Questions