Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting screenOrientation to portrait doesn't work

I want to run my app in portrait mode, I'm aware this isn't best practice but there are reasons to do this. and while I have disabled the rotation it still is able to rotate on some views will it doesn't on others.

I have this part of code in my Android Manifest:

 <activity
        android:name="<name>.app.MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboardHidden|keyboard|locale|orientation"
        android:screenOrientation="portrait">

I'm using fragments to show different containers depending on user input.

this is the only activity that has fragments. I have tried a few solutions on this site. including setting portrait mode on by code

like image 360
Rob de Heus Avatar asked Aug 19 '14 09:08

Rob de Heus


People also ask

How do I force an app into portrait mode?

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.

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 fix my portrait app?

You just have to define the property below inside the activity element in your AndroidManifest. xml file. It will restrict your orientation to portrait. Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.


1 Answers

You can do it something like below.

After rootView in your java add this line

 getActivity().setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  // programmatically

For example:

View rootView = inflater.inflate(R.layout.activityxml, container, false);       
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

And also in your manifest change it:

android:configChanges="orientation|keyboardHidden"

as

android:configChanges="keyboardHidden"

<activity
    android:name="com.test.activity"
    android:label="@string/app_name" 
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden" >
like image 179
Paras Valera Avatar answered Oct 06 '22 01:10

Paras Valera