Search code examples
androidxmlandroid-studioscrollviewhorizontalscrollview

Prevent HorizontalScrollView to be accessed from anywhere on the screen


I made (in Android Studio) a HorizontalScrollView which I can access from anywhere on the screen, meaning that where ever I scroll, be it on the top or the bottom of the screen, the ScrollView which is in the middle gets scrolled.

This is the code:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_marginRight="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1">

        <ImageButton
            android:id="@+id/somerandomid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/image"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="140dp" />
        <ImageButton
            android:id="@+id/somerandomid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/image"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="140dp" />
   </LinearLayout>
</HorizontalScrollView>

Solution

  • Please post all of the code of this layout, although I might suggest using a specific height for horizontalSerollView to protect it from match_parent or wrap_content and use RelativeLayout as child.

    <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="your_height(not match_parent)"
            android:id="@+id/horizontalScrollView"
            android:scrollbars="none">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">
    
        <ImageButton
            android:id="@+id/somerandomid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/image"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="140dp" />
    
        <ImageButton
            android:id="@+id/somerandomid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/image"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="140dp" />
    
            </RelativeLayout>
    
        </HorizontalScrollView>