Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the Orientation for fragments in Android?

I have managed to change the Orientation for my fragment in my activity. The Orientation is changed when my device is rotated.

Now I want to same functionality when I click on a button in the fragment. i.e. I want to change from portrait mode to LandScape mode when I click on a button on the portrait layout and visa vera.

I used

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

and

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

respectively.

But this made the auto rotate functionality stop working. How can I fix this issue?

like image 679
Nik Avatar asked Oct 10 '22 01:10

Nik


1 Answers

When you need auto rotating use getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

like image 181
mstrengis Avatar answered Oct 13 '22 11:10

mstrengis