Search code examples
androidandroid-fragmentsandroid-orientation

How to set different orientation of fragment in activity?


In my MainActivity has fragmentA, fragmentB, fragmentC.

I want set fragmentA and fragmentB orientation to portrait but fragmentC to landscape.

If the Manifest is set to portrait like this:

<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" />

all fragment orientations are portrait.

How would I set the different orieantation of each fragment?


Solution

  • Try this in OnCreateView(View view) of particular fragment,

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