HorizontalScrollView not working

6.3k views Asked by At

I am unable to scroll horizontally using HorizontalScrollView, but the same code in other files appears to work fine. I am unable to find an error in this file.

Here is the HorizontalScrollView xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    android:background="#87E886"
    android:orientation="horizontal">


    <HorizontalScrollView
        android:id="@+id/scrollView1"
        android:layout_width="2200dp"
        android:layout_height="550dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">



            <!--D2-->
            <LinearLayout
                android:layout_width="310dp"
                android:layout_height="520dp"
                android:background="#024500"
                android:layout_marginLeft="10dp">
                <RelativeLayout
                    android:layout_width="290dp"
                    android:layout_height="499dp"
                    android:orientation="horizontal"
                    android:layout_marginTop="10dp"
                    android:background="#BEFFB6"
                    android:layout_marginLeft="10dp"
                    android:weightSum="1">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageView16"
                        android:src="@drawable/des1"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true"
                        android:layout_marginTop="20dp" />
                    <ImageView
                        android:layout_width="220dp"
                        android:layout_height="190dp"
                        android:id="@+id/imageView17"
                        android:src="@drawable/img_bahay"
                        android:layout_below="@+id/imageView16"
                        android:layout_alignParentStart="true"
                        android:layout_marginLeft="35sp" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageView18"
                        android:src="@drawable/des_bahay"
                        android:layout_below="@+id/imageView17"
                        android:layout_alignParentStart="true" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="120dp"
                        android:id="@+id/imageView19"
                        android:layout_below="@+id/imageView18"
                        android:src="@drawable/desc_bahay"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentBottom="true" />

                </RelativeLayout>
            </LinearLayout>
            <ImageView
                android:layout_marginTop="180dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="30dp"
                android:layout_width="205dp"
                android:layout_height="100dp"
                android:id="@+id/xaxaw"
                android:src="@drawable/arrow"
                ></ImageView>

            <!--D3-->
            <LinearLayout
                android:layout_width="310dp"
                android:layout_height="520dp"
                android:background="#024500"
                android:layout_marginLeft="10dp">
                <RelativeLayout
                    android:layout_width="290dp"
                    android:layout_height="499dp"
                    android:orientation="horizontal"
                    android:layout_marginTop="10dp"
                    android:background="#BEFFB6"
                    android:layout_marginLeft="10dp"
                    android:weightSum="1">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewa16"
                        android:src="@drawable/des2"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true"
                        android:layout_marginTop="20dp" />
                    <ImageView
                        android:layout_width="220dp"
                        android:layout_height="190dp"
                        android:id="@+id/imageViewa17"
                        android:src="@drawable/img_chinatown"
                        android:layout_below="@+id/imageViewa16"
                        android:layout_alignParentStart="true"
                        android:layout_marginLeft="35sp" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewa18"
                        android:src="@drawable/des_chinatown"
                        android:layout_below="@+id/imageViewa17"
                        android:layout_alignParentStart="true" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="120dp"
                        android:id="@+id/imageViewa19"
                        android:src="@drawable/desc_chinatown"
                        android:layout_below="@+id/imageViewa18"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentBottom="true" />
                    <!--      <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="Estimated Duration:"
                             android:id="@+id/textView13"
                             android:textColor="#000000"
                             android:layout_below="@+id/imageView19"
                             android:layout_alignParentStart="true"
                             android:layout_marginTop="10dp"
                             android:layout_marginLeft="10dp" />
                         <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="New Text"
                             android:id="@+id/textView14"
                             android:textColor="#000000"
                             android:layout_below="@+id/textView13"
                             android:layout_alignParentStart="true"
                             android:layout_marginLeft="35dp" />
                         <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="Estimated Distance:"
                             android:id="@+id/textView15"
                             android:textColor="#000000"
                             android:layout_alignTop="@+id/textView13"
                             android:layout_alignParentEnd="true"
                             android:layout_marginRight="15dp" />
                         <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="New Text"
                             android:id="@+id/textView16"
                             android:textColor="#000000"
                             android:layout_marginLeft="35dp"
                             android:layout_below="@+id/textView15"
                             android:layout_alignStart="@+id/textView15"/> -->
                </RelativeLayout>
            </LinearLayout>

            <ImageView
                android:layout_marginTop="180dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="30dp"
                android:layout_width="205dp"
                android:layout_height="100dp"
                android:id="@+id/arrow1"
                android:src="@drawable/arrow"
                ></ImageView>


            <!-- D4-->
            <LinearLayout
                android:layout_width="310dp"
                android:layout_height="520dp"
                android:background="#024500"
                android:layout_marginLeft="10dp">
                <RelativeLayout
                    android:layout_width="290dp"
                    android:layout_height="499dp"
                    android:orientation="horizontal"
                    android:layout_marginTop="10dp"
                    android:background="#BEFFB6"
                    android:layout_marginLeft="10dp"
                    android:weightSum="1">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewaa16"
                        android:src="@drawable/des3"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true"
                        android:layout_marginTop="20dp" />
                    <ImageView
                        android:layout_width="220dp"
                        android:layout_height="190dp"
                        android:id="@+id/imageViewaa17"
                        android:src="@drawable/img_goodwill"
                        android:layout_below="@+id/imageViewaa16"
                        android:layout_alignParentStart="true"
                        android:layout_marginLeft="35sp" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewaa18"
                        android:src="@drawable/des_goodwill"
                        android:layout_below="@+id/imageViewaa17"
                        android:layout_alignParentStart="true" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="120dp"
                        android:id="@+id/imageViewaa19"
                        android:src="@drawable/desc_goodwill"
                        android:layout_below="@+id/imageViewaa18"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentEnd="true" />
                </RelativeLayout>
            </LinearLayout>
            <ImageView
                android:layout_marginTop="180dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="30dp"
                android:layout_width="205dp"
                android:layout_height="100dp"
                android:id="@+id/arrow53"
                android:src="@drawable/arrow"
                ></ImageView>



            <!--D5-->
            <LinearLayout
                android:layout_width="310dp"
                android:layout_height="520dp"
                android:background="#024500"
                android:layout_marginLeft="10dp">
                <RelativeLayout
                    android:layout_width="290dp"
                    android:layout_height="499dp"
                    android:orientation="horizontal"
                    android:layout_marginTop="10dp"
                    android:background="#BEFFB6"
                    android:layout_marginLeft="10dp"
                    android:weightSum="1">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewaag16"
                        android:src="@drawable/des4"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentStart="true"
                        android:layout_marginTop="20dp" />
                    <ImageView
                        android:layout_width="220dp"
                        android:layout_height="190dp"
                        android:id="@+id/imageViewaag17"
                        android:src="@drawable/img_chcemetery"
                        android:layout_below="@+id/imageViewaag16"
                        android:layout_alignParentStart="true"
                        android:layout_marginLeft="35sp" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:id="@+id/imageViewaag18"
                        android:src="@drawable/des_chcemetery"
                        android:layout_below="@+id/imageViewaag17"
                        android:layout_alignParentStart="true" />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="120dp"
                        android:id="@+id/imageViewaag19"
                        android:src="@drawable/desc_chcemetery"
                        android:layout_below="@+id/imageViewaag18"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentEnd="true" />
                </RelativeLayout>
            </LinearLayout>



    </HorizontalScrollView>
</LinearLayout>
3

There are 3 answers

0
Devil10 On BEST ANSWER

Make HorizontalScrollView's with to "wrap_content" and maintain a single child to the HorizontalScrollView i.e.

<HorizontalScrollView
  android:id="@+id/scrollView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >

  <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">

       rest of the code here...... 

  </LinearLayout>
</HorizontalScrollView>
0
Nilabja On

it seems like you have forgotten to end the LinearLayout tag before ending HorizontalScrollView . Your code should look like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

android:background="#87E886"
android:orientation="horizontal">


<HorizontalScrollView
    android:id="@+id/scrollView1"
    android:layout_width="2200dp"
    android:layout_height="550dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">



        <!--D2-->
        <LinearLayout
            android:layout_width="310dp"
            android:layout_height="520dp"
            android:background="#024500"
            android:layout_marginLeft="10dp">
            <RelativeLayout
                android:layout_width="290dp"
                android:layout_height="499dp"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:background="#BEFFB6"
                android:layout_marginLeft="10dp"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageView16"
                    android:src="@drawable/des1"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="20dp" />
                <ImageView
                    android:layout_width="220dp"
                    android:layout_height="190dp"
                    android:id="@+id/imageView17"
                    android:src="@drawable/img_bahay"
                    android:layout_below="@+id/imageView16"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="35sp" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageView18"
                    android:src="@drawable/des_bahay"
                    android:layout_below="@+id/imageView17"
                    android:layout_alignParentStart="true" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="120dp"
                    android:id="@+id/imageView19"
                    android:layout_below="@+id/imageView18"
                    android:src="@drawable/desc_bahay"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true" />

            </RelativeLayout>
        </LinearLayout>
        <ImageView
            android:layout_marginTop="180dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="30dp"
            android:layout_width="205dp"
            android:layout_height="100dp"
            android:id="@+id/xaxaw"
            android:src="@drawable/arrow"
            ></ImageView>

        <!--D3-->
        <LinearLayout
            android:layout_width="310dp"
            android:layout_height="520dp"
            android:background="#024500"
            android:layout_marginLeft="10dp">
            <RelativeLayout
                android:layout_width="290dp"
                android:layout_height="499dp"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:background="#BEFFB6"
                android:layout_marginLeft="10dp"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewa16"
                    android:src="@drawable/des2"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="20dp" />
                <ImageView
                    android:layout_width="220dp"
                    android:layout_height="190dp"
                    android:id="@+id/imageViewa17"
                    android:src="@drawable/img_chinatown"
                    android:layout_below="@+id/imageViewa16"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="35sp" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewa18"
                    android:src="@drawable/des_chinatown"
                    android:layout_below="@+id/imageViewa17"
                    android:layout_alignParentStart="true" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="120dp"
                    android:id="@+id/imageViewa19"
                    android:src="@drawable/desc_chinatown"
                    android:layout_below="@+id/imageViewa18"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentBottom="true" />
                <!--      <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="Estimated Duration:"
                         android:id="@+id/textView13"
                         android:textColor="#000000"
                         android:layout_below="@+id/imageView19"
                         android:layout_alignParentStart="true"
                         android:layout_marginTop="10dp"
                         android:layout_marginLeft="10dp" />
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="New Text"
                         android:id="@+id/textView14"
                         android:textColor="#000000"
                         android:layout_below="@+id/textView13"
                         android:layout_alignParentStart="true"
                         android:layout_marginLeft="35dp" />
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="Estimated Distance:"
                         android:id="@+id/textView15"
                         android:textColor="#000000"
                         android:layout_alignTop="@+id/textView13"
                         android:layout_alignParentEnd="true"
                         android:layout_marginRight="15dp" />
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="New Text"
                         android:id="@+id/textView16"
                         android:textColor="#000000"
                         android:layout_marginLeft="35dp"
                         android:layout_below="@+id/textView15"
                         android:layout_alignStart="@+id/textView15"/> -->
            </RelativeLayout>
        </LinearLayout>

        <ImageView
            android:layout_marginTop="180dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="30dp"
            android:layout_width="205dp"
            android:layout_height="100dp"
            android:id="@+id/arrow1"
            android:src="@drawable/arrow"
            ></ImageView>


        <!-- D4-->
        <LinearLayout
            android:layout_width="310dp"
            android:layout_height="520dp"
            android:background="#024500"
            android:layout_marginLeft="10dp">
            <RelativeLayout
                android:layout_width="290dp"
                android:layout_height="499dp"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:background="#BEFFB6"
                android:layout_marginLeft="10dp"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewaa16"
                    android:src="@drawable/des3"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="20dp" />
                <ImageView
                    android:layout_width="220dp"
                    android:layout_height="190dp"
                    android:id="@+id/imageViewaa17"
                    android:src="@drawable/img_goodwill"
                    android:layout_below="@+id/imageViewaa16"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="35sp" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewaa18"
                    android:src="@drawable/des_goodwill"
                    android:layout_below="@+id/imageViewaa17"
                    android:layout_alignParentStart="true" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="120dp"
                    android:id="@+id/imageViewaa19"
                    android:src="@drawable/desc_goodwill"
                    android:layout_below="@+id/imageViewaa18"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
        </LinearLayout>
        <ImageView
            android:layout_marginTop="180dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="30dp"
            android:layout_width="205dp"
            android:layout_height="100dp"
            android:id="@+id/arrow53"
            android:src="@drawable/arrow"
            ></ImageView>



        <!--D5-->
        <LinearLayout
            android:layout_width="310dp"
            android:layout_height="520dp"
            android:background="#024500"
            android:layout_marginLeft="10dp">
            <RelativeLayout
                android:layout_width="290dp"
                android:layout_height="499dp"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:background="#BEFFB6"
                android:layout_marginLeft="10dp"
                android:weightSum="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewaag16"
                    android:src="@drawable/des4"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="20dp" />
                <ImageView
                    android:layout_width="220dp"
                    android:layout_height="190dp"
                    android:id="@+id/imageViewaag17"
                    android:src="@drawable/img_chcemetery"
                    android:layout_below="@+id/imageViewaag16"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="35sp" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewaag18"
                    android:src="@drawable/des_chcemetery"
                    android:layout_below="@+id/imageViewaag17"
                    android:layout_alignParentStart="true" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="120dp"
                    android:id="@+id/imageViewaag19"
                    android:src="@drawable/desc_chcemetery"
                    android:layout_below="@+id/imageViewaag18"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>


</HorizontalScrollView>

0
abhi On

ScrollView or in your case HorizontalScrollView can only have one direct child view. Like :

 <ScrollView>

<LinearLayout>

// add all the required views here. 

</Linearlayout>

</Scrollview>

Although, you can add mulitiple views inside that Linear or any other layout.