How to change orientation for fragments in Android?

I managed to change the orientation for my fragment in my activity. Orientation changes when my device is rotated.

Now I want to use the same functionality when I click a button in a snippet. that is, I want to switch from portrait mode to LandScape mode when I press the button on the portrait layout and vera visa.

I used

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

and

  getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

respectively.

But this led to the fact that the automatic rotation function stopped working. How can I fix this problem?

+4
source share
2 answers

If you need automatic rotation, use getActivity().setRequestedOrientation(ActivityInfo.ActivityInfo.SCREEN_ORIENTATION_SENSOR);

+3
source

Please, try

  <activity android:name="<class name with package>" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"> 
0
source

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


All Articles