How to lock android app's orientation to portrait mode?

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.

  • List item with button not clickable anymore
  • Are Android geofences surviving a reboot?
  • Android ProgressBar UI custom layout
  • jQuery click event not working in mobile browsers
  • Icon in Tab is not showing up
  • Is there any way to convert JSONP format to JSON?
  • Android transparent overlay toolbar
  • Android - Can SQLite Cursor's be used after closing the database?
  • How can I return to a parent activity correctly?
  • What benefits does Maven give (over ant) for building android projects?
  • Swipe/Fling tab-changing in conjunction with ScrollView?
  • How can I implement Google Play Licensing for an Android app?
  • 6 Solutions collect form web for “How to lock android app's orientation to portrait mode?”

    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>
    

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

    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" />.

    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.

    It might be.. you have to identify it is tablet or phone by programmatically…

    if(isTablet)
    {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);      
    }else
    {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    <activity android:name=".yourActivity"
              android:screenOrientation="portrait" ... />
    

    add to main activity and add

    android:configChanges="keyboardHidden"
    

    to keep your program from changing mode when keyboard is called.

    Set the Screen orientation to portrait in Manifest file under the activity Tag.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.