In my layout, I have to show three rows in portrait and 2 in landscape mode. I have to add multiple imageviews in these rows at runtime. My these rows are in a HorizontalScrollView. When the orientation changes, I remove all views from the rows, set rows count (2 or 3) and then again add images to the visible rows. Suppose I was first in portrait mode, I rotate the device to landscape mode, then contents are placed properly. But when again I take device portrait mode, the rows leaves empty extra space at the end. My layout xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="15dp"
android:tag="scrollView"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:id="@+id/shelf1_container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:layout_weight="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/shelf1_base"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scaleType="fitXY"
android:src="@drawable/shelf_bg" />
<LinearLayout
android:id="@+id/shelf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="bottom"
android:orientation="horizontal"
android:tag="shelf1" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/shelf2_container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:layout_weight="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/shelf2_base"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scaleType="fitXY"
android:src="@drawable/shelf_bg" />
<LinearLayout
android:id="@+id/shelf2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="bottom"
android:orientation="horizontal"
android:tag="shelf1" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/shelf3_container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:layout_weight="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/shelf3_base"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scaleType="fitXY"
android:src="@drawable/shelf_bg" />
<LinearLayout
android:id="@+id/shelf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:gravity="bottom"
android:orientation="horizontal"
android:tag="shelf1" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I think the problem is that when there are two rows (landscape mode), the rows have more width and when it comes to portrait mode, the contents are divided in three rows, the first 2 rows width remain same. Even though, I am removing all views from the rows and then add the new ones. The rows width should have been reduced according to contents. Also the layout is inside a fragment and I have added the following in activity manifest android:configChanges="orientation|keyboardHidden|screenSize"
to prevent fragment recreation.
Any idea where I am going wrong?