Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I change the orientation of a fragment?

Use the following code line in the fragment where you want a specific (in this case portrait) orientation. getActivity(). setRequestedOrientation( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);

How do I open the fragment in landscape mode?

setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); The fragment does load in landscape mode.


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.


In each of your fragments, set the requested orientation.

Reference doc: http://developer.android.com/reference/android/content/pm/ActivityInfo.html

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   // Fragment locked in portrait screen orientation
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    

   // Fragment locked in landscape screen orientation
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

   // Fragment screen orientation normal both portait and landscape       
   getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

Orientation attribute is per activity so you can declare the orientation for only the activity that contains the fragment so that it is in landscape and the rest of the activities will remain as they are.


So I'm dealing with this issue now. We have only portrait mode application (for now). But there is one fragment that needs to be in landscape. We are using single Activity approach so the accepted answer will not help me.

The fastest solution I could think of is this one.

private var swappingOrientation = false

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if(savedInstanceState == null) {
        swappingOrientation = true
        activity?.apply {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }
}

override fun onDestroy() {
    super.onDestroy()

    if(!swappingOrientation) {
        activity?.apply {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }
    swappingOrientation = false
}

You hold the information if you are swapping orientation or not in swappingOrientation variable. At the beggining when the fragment is created it will change orientation, only when there is no saved state. And orientation is changed back again only when it is not being currently changed.

This is a super quick solution and it can produce screen blinking when you return to previous fragment. I also did not fully test it so it can have other issues, so keep that in mind.