J4C3N-14
J4C3N-14

Reputation: 696

Android: Two images inside a scroll view

I would like to be able to display 2 images to a user, however I would like the images to fill the screen (horizontally) and then be able to scroll down to the next image, is this possible? I have been messing around with different XML layouts and cant seem to find a solution. When I place an image view inside of a scroll view it doesn't fill the screen, how can I get 2 in there but only 1 visible to start off with?

 ?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/building_block"
    android:layout_margin="2dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/ic_ntm"
            android:id="@+id/imageView15" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/ic_ntm"
            android:id="@+id/imageView17"
            android:layout_marginTop="44dp" />

    </ScrollView>


</RelativeLayout>

That is what I would like but is it possible. 1 image under the other each filling the screen and in a scrollview?

Upvotes: 2

Views: 1697

Answers (2)

Roman Lebedenko
Roman Lebedenko

Reputation: 29

I think I need to set your images height programmatically to be equal with screen height.

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
</ScrollView>

Upvotes: 0

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

You cant add more than on child to a ScrollView (or HorizontalScrolView).

You can fix it with a child layout to have both elements.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true">
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        android:gravity="center"
        android:scaleType="fitXY"
        android:src="@drawable/ic_ntm"
        android:id="@+id/imageView15" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:gravity="center"
        android:scaleType="fitXY"
        android:layout_weight="1"
        android:src="@drawable/ic_ntm"
        android:id="@+id/imageView17"
        android:layout_marginTop="44dp" />
     </LinearLayout>
</ScrollView>

Also note the android:layout_weight="1", so each ImageView can take half of the view, cause if both are fill_parent only the first will show.

Upvotes: 1

Related Questions