Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow rotation/landscape in one fragment

My app has a single Activity with a FragmentPagerAdapter with four fragments (Using the ViewPagerIndicator library). One of these fragments has designs for both a separate portrait and landscape layout, the other three do not and need to be fixed to portrait orientation.

My thought was to set android:configChanges="orientation" in the manifest and call getActivity().setRequestedScreenOrientation() in the onResume() of all the fragments, locking to SCREEN_ORIENTATION_PORTRAIT in three of them but to SCREEN_ORIENTATION_UNSPECIFIED in the one that needs to allow rotation, but this doesn't work. The app remains in portrait mode.

Is there a way to achieve this?

It isn't actually necessary for the actual activity to rotate if there is anyway to allow a fragment to change orientation without its activity doing so, but I have not found anything mentioning this being possible. It would also be equally ok if the activity rotates since the tab bar will be hidden when in landscape orientation.

like image 350
Garcon Avatar asked Nov 06 '12 13:11

Garcon


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 to landscape fragment in android?

I would use getActivity(). setRequestedScreenOrientation(SCREEN_ORIENTATION_PORTRAIT) for the portrait fragments. And getActivity(). setRequestedScreenOrientation(SCREEN_ORIENTATION_LANDSCAPE) for the landscape fragments.

How do I open the fragment in landscape mode?

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

Can you use a fragment in more than one activity?

You can use a Fragment in more than one Activity . One Activity can have multiple fragments. After you define a Fragment in a Kotlin class, the fragment is automatically added to the activity_main.

How to set orientation of a fragment in landscape mode?

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. Use the following code line in the fragment where you want a specific (in this case portrait) orientation.

Is it possible to rotate the activity without the fragment?

3 Then it is not possible, you cannot just rotate the activity without the fragment. You can just lock the orientation at certain fragments. – Kevin van Mierlo Dec 5 '13 at 8:04

How to lock the orientation of visible fragments?

All the solutions to locking a fragment orientation suggest to use setRequestedOrientation and lock the activity orientation, but I need other visible fragments to rotate. My app supports API 10+ (if there is a nice solution that uses API 11+ I may consider removing support for landscape in API <11). Thanks in advance.

How to get activity from a fragment in Java?

In case for fragment in activity means i guess you can you below code getActivity ().setRequestedOrientation ( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in activity i.e write a method by passing boolean if true PORTRAIT else LANDSCAPE and call it from fragment.


4 Answers

Override setUserVisibleHint() in each fragment.

In the portrait only fragments:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

in the the portrait/landscape fragment:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
}

This will allow the whole activity to rotate in one fragment, but fix it to portrait in others.

like image 178
Garcon Avatar answered Oct 18 '22 18:10

Garcon


One thing that worked for me was to just put

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

At the top of the OnCreateView() methods for each of the fragments. You would want to replace ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR with the appropriate SCREEN_ORIENTATION constant.

Once I did this it worked perfectly on ICS. You don't event have to adjust the manifest for the specific activity.

like image 42
George Baker Avatar answered Oct 18 '22 18:10

George Baker


Issue is if you enable configChanges you then need to handle onConfigurationChanged() method in your activity/fragments.

Meaning that if you did fire the getActivity().setRequestedScreenOrientation() you would have to manually call setContentView() again to reinflate the landscape layout.

Also setting `UNSPECIFIED' will not change to landscape, it will just remain where it is.

I would use getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_PORTRAIT) for the portrait fragments. And getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_LANDSCAPE) for the landscape fragments.

This will reinflate the activity layout, which means you will then need to keep track of the last ViewPager page you where on, to make sure you show that after the layout has been recreated as to default back to that fragment before they are shown to the user and fragment onResume() is called.

Its going to be fiddly but, it is possible.

like image 4
Chris.Jenkins Avatar answered Oct 18 '22 18:10

Chris.Jenkins


Write some code in AndroidManifest.xml on the particular activity tag.

android:configChanges="orientation|screenSize".

And Write code in Fragment on onCreateView method,
for Portrait:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
setRetainInstance(true);

for Landscape:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRetainInstance(true);
like image 1
Virendra Kachhi Avatar answered Oct 18 '22 19:10

Virendra Kachhi