Rob de Heus
Rob de Heus

Reputation: 73

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

Upvotes: 7

Views: 10281

Answers (4)

Paras Valera
Paras Valera

Reputation: 492

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

Upvotes: 7

Yavuz Erg&#252;l
Yavuz Erg&#252;l

Reputation: 1

add Android Manifest:

tools:ignore="LockedOrientationActivity"
android:screenOrientation="portrait">

Upvotes: 0

Elango
Elango

Reputation: 411

try this code

 <activity android:name="com.myActivity" android:screenOrientation="portrait"
                android:configChanges="keyboardHidden|orientation"
                >
            </activity>

Upvotes: 1

MHP
MHP

Reputation: 2731

use below code before super.onCreate.first line of code.it force activity to portrait(of course both side of portrait)

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);  

the code that you entered android:configChanges="screenSize|orientation" just force activity to don't miss it's stats on rotate(onCreate call just once and if rotate onPause will call)

Upvotes: 0

Related Questions