Search code examples
androidandroid-layoutandroid-scrollviewhorizontalscrollview

How can I get my imageView to scroll horizontally as well as vertically?


I have an image which contains text and displays fine in larger screen devices but on smaller screens the text cannot be read. I decided to use one size and scroll on the smaller screens. I used the code below to set both vertical scrolling and horizontal scrolling but it will only scroll vertically.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="820px"
        android:layout_height="fill_parent">
        <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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".CategoriesActivity">
.
.
.
            <ImageView
                android:id="@+id/termsImg"
                android:layout_width="450dp"
                android:layout_height="600dp"
                android:layout_below="@+id/headerPanel"
                android:background="@drawable/screen1" />
    </HorizontalScrollView>
</ScrollView>

The horizontal scrolling works on larger screens but not on 240 or 320 size screens.

What do I need to change to allow my imageView to scroll horizontally as well as vertically?


Solution

  • Just do this.

    <ScrollView
        android:id="@+id/layout0"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/layout1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <HorizontalScrollView
                android:id="@+id/layout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <LinearLayout
                    android:id="@+id/layout3"
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <ImageView
                        <!-- Your ImageVIew -->
                    </ImageVIew>
                </LinearLayout>
    
            </HorizontalScrollView>
    
        </LinearLayout>
    
    </ScrollView>
    

    But as you see, it seems very unefficient. You can solve your problem by scale your image view to a appropriate scaleType and using one ScrollView.