Screen setup Portrait orientation not working

I want to run the application in portrait mode, I know that this is not the best practice, but there are reasons for this. and while I turned off the rotation, it can still rotate in some views, whether it will be different.

I have this piece of code in Android Manifest:

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

I use fragments to display different containers depending on user input.

This is the only action in which there are fragments. I tried several solutions on this site. including setting portrait mode by code

+6
source share
3 answers

you can do something like below

After rootView in java add this line

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

For Ex:

 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" > 
+6
source

use code lower than super.onCreate.first line code.it forces activity to the portrait (of course, both sides of the portrait)

 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 

the android code you entered: configChanges = "screenSize | orientation" just makes the activity not to miss its statistics when rotating (onCreate call only once and when rotating onPause will call)

0
source

try this code

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

Source: https://habr.com/ru/post/974018/


All Articles