Search code examples
androidxmlandroid-layouthorizontalscrollview

Linear Layout or Horizontal Scroll View is cutting off content


I'm trying to achieve a simple screen where I have a horizontal scroll view with book entries. The problem I'm having is that it seems to cut off prematurely, cutting off entries. I have looked at other questions similar to this, and their fixes don't seem to fix mine, so I'm not sure how to handle this.

Here is what the cutting off looks like:

enter image description here

And here is my code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#D3D3D3" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="@string/af"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#A1A1A1" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_horizontal|end"
                android:orientation="horizontal" >
                <RelativeLayout
                    android:id="@+id/relative1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="168dp"
                        android:layout_height="258dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="5dp"
                        android:contentDescription="@string/cd"
                        android:src="@drawable/af1" />
                    <TextView
                        android:id="@+id/textLabel1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/imageView1"
                        android:layout_centerInParent="true"
                        android:singleLine="true"
                        android:text="Guilty Wives" />
                </RelativeLayout>
                /*RelativeLayout repeats with different data, cut for brevity*/
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</ScrollView>

Solution

  • this is a known bug that has never been solved: (https://code.google.com/p/android/issues/detail?id=20088), but here is the code that worked for me. the trick is to set the horizontal scroll view's width to wrap content and the width of the linear (or relative) layout below it to match parent .

    <HorizontalScrollView
        android:id="@+id/group_scroller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >
    
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <RadioGroup
            android:id="@+id/choices_group"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
    
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal" />
        </LinearLayout>
    </HorizontalScrollView>