Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?

I am developing an Android app whose orientation I don't want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait mode on phones and landscape mode on tablets. Can this be achieved, if yes how? Thanks.

like image 229
Ankit Rawat Avatar asked Mar 30 '13 13:03

Ankit Rawat


People also ask

How do I set android apps to portrait only?

If you want to develop an application in portrait mode, add screenOrientation inside application tag. In the above result it is showing only portrait mode. now turn your device it not going to change the view according to orientation.

How do I lock my app orientation?

On the main screen of Rotation Manager, select an orientation by tapping on either the vertical or horizontal icons next to a specific app to lock it into either landscape or portrait mode. Highlighting both icons will allow that particular app to auto-rotate.


3 Answers

You just have to define the property below inside the activity element in your AndroidManifest.xml file. It will restrict your orientation to portrait.

android:screenOrientation="portrait"

Example:

        <activity
            android:name="com.example.demo_spinner.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

if you want this to apply to the whole app define the property below inside the application tag like so:

        <application>
         android:screenOrientation="sensorPortrait"
        </application>

Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.

like image 172
RobinHood Avatar answered Oct 16 '22 11:10

RobinHood


You have to add the android:screenOrientation="portrait" directive in your AndroidManifest.xml. This is to be done in your <activity> tag.

In addition, the Android Developers guide states that :

[...] you should also explicitly declare that your application requires either portrait or landscape orientation with the element. For example, <uses-feature android:name="android.hardware.screen.portrait" />.

like image 41
Halim Qarroum Avatar answered Oct 16 '22 11:10

Halim Qarroum


I can see you have accepted an answer which doesn't solve your problem entirely:

android:screenOrientation="portrait" 

This will force your app to be portrait on both phones and tablets.

You can have the app forced in the device's "preferred" orientation by using

android:screenOrientation="nosensor"

This will lead to forcing your app to portrait on most phones phones and landscape on tablets. There are many phones with keypads which were designed for landscape mode. Forcing your app to portrait can make it almost unusable on such devices. Android is recently migrating to other types of devices as well. It is best to just let the device choose the preferred orientation.

like image 25
Radu Simionescu Avatar answered Oct 16 '22 12:10

Radu Simionescu