0

In my Activity I have a View that "depends" from screen orientation. If it's in landscape mode, i use a layout under layout-large-hand, but if it's in portrait mode, I use the layouts under the layouts folder. The Activity shows also a Map with some markers and informations.

In my AndroidManifest i have

android:configChanges="orientation|screenSize"

But the activity shows only the layout on portrait mode.

How can I fix this?

EDIT 3/12:

I have a layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

<include
    android:id="@+id/my_view"
    layout="@layout/my_view"
    android:visibility="gone" />

</RelativeLayout>

and i want that my_view changes the layout, but map remains with all markers, zoom level, position, ecc ecc...

my_view is visible when i click on a Button created dinamically in the activity.

EDIT 2:

Like SweetWisher ツ says, i was trying to setup a custom behaviour for the views. But when i rotate the device, the map disappear. This is part of my code in the activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initUI();
}

private void initUI() {
    setContentView(R.layout.my_layout);

    if (mp == null) {
        mp = MapFragment.newInstance();
    }
    getFragmentManager().beginTransaction().replace(R.id.placeholder, mp).commit();

    initUIStuff()
}

private void initUIStuff(){

}



@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    initUI();

}

@Override
protected void onStart() {
    super.onStart();
    initGMap();
}

@Override
protected void onResume() {

    super.onResume();
    initGMap();
}

private void initGMap() {
    if (mp != null  {
        //Initialize Map
    }
}
Garro88
  • 360
  • 3
  • 12
  • [read this](http://stackoverflow.com/a/13848525/2591002) and [follow this](http://stackoverflow.com/a/13938015/2591002) – SweetWisher ツ Dec 01 '14 at 16:10
  • I have tried to follow the guide. I added some informations in the post. But when i click the button, the my_view had the same layout. – Garro88 Dec 03 '14 at 11:32

1 Answers1

0

If you use android:configChanges="orientation" in your Manifest you're preventing Android from doing default reset of view hierarchy on screen orientation change. You have to manually change your views in onConfigurationChanged method and by that I mean inflate your desired layout and replace your old layout with it. If you want to take advantage of Android automatic view hierarchy reset don't use android:configChanges="orientation".

Edit: Use following code to add map fragment through XML:

<fragment
  android:id="@+id/map"
  android:name="com.google.android.gms.maps.MapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

You also don't need now 'onConfigurationChange' method so delete it.

michal.z
  • 2,025
  • 1
  • 15
  • 10
  • So if i want to use a different layout for a single view if i'm in landscape or portrait mode, i have to manage all the views behaviour? Anyway i tried to remove the 'android:configChanges' tag in the manifest, but i view always the same layout. – Garro88 Dec 03 '14 at 11:57
  • 1
    If you'll keep using `android:configChanges` then yes you have to manage all the views manually. Without `android:configChanges` Android OS will do views stuff for you. I think you can't notice a difference even after removing tag because names of your layout directories are improper. Put your landscape layout in /res/layout-land and your portrait layout in /res/layout. – michal.z Dec 03 '14 at 12:02
  • Ooooops... the landscape layout's folder was layouts-large-land and not layouts-land. Now it works! But the map is not showing after rotation. See my updated post. – Garro88 Dec 03 '14 at 14:19
  • If you want to add fragment through XML use `android:name="com.google.android.gms.maps.MapFragment"` in your fragment tag. It is sufficient to create and add fragment to Activity so don't perform any additional fragment transactions like you do in `initUI()`. – michal.z Dec 03 '14 at 14:57
  • If i add the fragment though XML, and i rotate the device i got an `android.view.InflateException: Binary XML file line #8: Error inflating class fragment` on `setContentView(R.layout.my_layout)`. – Garro88 Dec 03 '14 at 16:21