swagswag
swagswag

Reputation: 31

RelativeLayout inside ScrollView doesn't scroll. What i'm doing wrong?

I'm trying to make scrollable a landscape layout. I put my RelativeLayout inside a ScrollView, but nothing happens. Here's the code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:gravity="center_horizontal">

<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:background="@color/green_light"
    tools:context="app.example.ui.SplashActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/parent_center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo" />

    <View
        android:id="@+id/parent_center"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <LinearLayout
        android:id="@+id/ll_gateway_discovery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/parent_center"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/registration"
            android:textSize="@dimen/text_large"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/who_are_you"
            android:textColor="@color/grey_dark"
            android:textSize="@dimen/text_normal"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_username"
            android:layout_margin="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="240dp"
            android:hint="@string/prompt_email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:singleLine="true"
            android:textSize="@dimen/text_normal" />

        <EditText
            android:id="@+id/et_password"
            android:layout_margin="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="240dp"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:text="pippo"
            android:hint="@string/password"
            android:textSize="@dimen/text_normal" />

        <ImageView
            android:id="@+id/btn_login"
            android:layout_marginTop="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_submit" />
    </LinearLayout>
</RelativeLayout>

What I'm doing wrong? It is driving me insane :)

Upvotes: 2

Views: 727

Answers (3)

ben10
ben10

Reputation: 190

Use Linearlayout instead of relativeLayout

Upvotes: 1

Xaver Kapeller
Xaver Kapeller

Reputation: 49807

The problem is you are using match_parent instead of wrap_content as layout_width and layout_height for the RelativeLayout. For example if you want to scroll vertically you have to use this:

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

With this the RelativeLayout will have the same width as the ScrollView but the height will be as big as the views inside it.

Resulting xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:gravity="center_horizontal" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green_light"
        tools:context="app.example.ui.SplashActivity" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/parent_center"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/logo" />

        <View
            android:id="@+id/parent_center"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerInParent="true" />

        <LinearLayout
            android:id="@+id/ll_gateway_discovery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/parent_center"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/registration"
                android:textSize="@dimen/text_large"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/who_are_you"
                android:textColor="@color/grey_dark"
                android:textSize="@dimen/text_normal"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/et_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:hint="@string/prompt_email"
                android:inputType="textEmailAddress"
                android:maxLines="1"
                android:minWidth="240dp"
                android:singleLine="true"
                android:textSize="@dimen/text_normal" />

            <EditText
                android:id="@+id/et_password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:hint="@string/password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:minWidth="240dp"
                android:singleLine="true"
                android:text="pippo"
                android:textSize="@dimen/text_normal" />

            <ImageView
                android:id="@+id/btn_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:background="@drawable/btn_submit" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

Upvotes: 0

Shubhank
Shubhank

Reputation: 21805

use HorizontalScrollView instead of ScrollView as normal ScrollView is vertical scroll only

and with scrollView child to be wrapContent in width/height .. for horizontalScrollView width

Upvotes: 0

Related Questions