1

I have a MapFragment class, which will take up the full screen upon calling it.

From this question, I understand that forcing a horizontal layout will require changes in the Android Manifest. However, how can I achieve this if it's for a MapFragment class?

This is my xml for my map display, if it helps:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/fragment_map"
    android:name=".com.fragments.MapFragment"
    class="com.fragments.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="tag_fragment_map" />

</FrameLayout>
Community
  • 1
  • 1
lyk
  • 1,578
  • 5
  • 25
  • 49

3 Answers3

4

You can extend the MapFragment class. When onAttach() is invoked store the current orientation of an activity ( by calling getActivity().getRequestedOrientation() ) to an integer then set the parent's orientation to landscape. This is temporary. If your MapFragment is no longer used it will set the parent's old orientation.

public class DummyMapFragment extends MapFragment
{

    private int activityOrientation;

    @Override
    public void onAttach(Activity arg0) {

        super.onAttach(arg0);

        activityOrientation = arg0.getRequestedOrientation();

                 arg0.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }


    @Override
    public void onStop() {
        Activity parent = getActivity();

        if(parent != null)
                    getActivity().setRequestedOrientation(activityOrientation);

        super.onStop();

    }
}
Glenn
  • 12,741
  • 6
  • 47
  • 48
  • Thank you for this! Seems to have helped to solve my problem! =) – lyk Feb 11 '13 at 07:59
  • Hi sorry, I just realized that once i do a .replace() to change the mapfragment to another fragment, the landscape position remains... is there a way to change it back to normal? – lyk Feb 11 '13 at 08:32
  • Can you try onStop() instead of onDestroy() ?? – Glenn Feb 11 '13 at 09:08
  • It sort of works now, but problem is when i got to the 2nd page and when i pressed the back button, the map is in protrait mode again, and the header which I removed in the beginning becomes visible again – lyk Feb 11 '13 at 09:37
1

Try this:

getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_LANDSCAPE)

Also check this link.

Community
  • 1
  • 1
user1744952
  • 508
  • 1
  • 3
  • 15
1

define activity in manifest file like this

<activity
        android:name=".yourMapActivity"
        android:label="@string/title_activity_edit_profile"
        android:screenOrientation="landscape" >
Krishna Shrestha
  • 1,662
  • 14
  • 38
  • Fragment always attached within the activity. i suppose you have activity where your FrameLayout is attached with and with in which DummyMapFragment should be attached. – Krishna Shrestha Feb 11 '13 at 08:29
  • But I only want my mapfragment to be landscape. i don't want my other fragments to be landscape once i replace it. – lyk Feb 11 '13 at 08:31
  • Then You should check which which fragment is being displayed. and if it is MapFragment den programatically set screen orientation to landscape. – Krishna Shrestha Feb 11 '13 at 09:57