Setting the orientation for only 1 fragment in my activity while the rest is in portrait

John Ubonty picture John Ubonty · Oct 3, 2012 · Viewed 38.9k times · Source

My app needs to be in portrait mode so I set it in the manifest by:

android:screenOrientation="portrait"

But I just recently added another fragment (FragA) that just looks and functions 10x better in landscape. Is there something I can put inside of my FragA to just make that fragment in landscape while retaining the rest of the app in portrait or by doing this will I have to add something to my other fragments to keep them retained as portrait?

Answer

pradeep.k picture pradeep.k · Oct 15, 2013

Use the following code line in the fragment where you want a specific (in this case portrait) orientation.

getActivity().setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

If you want to have a orientation in a fragment, that is based on the way the user holds his device, then use the following code line.

getActivity().setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

Hope, this will give you the intended solution.