Jemshit
Jemshit

Reputation: 10058

ScrollView with sub RelativeLayout does not scroll

I have a ScrollView with 1 RelativeLayout which has 3 sub RelativeLayouts:

enter image description here

I set top RelativeLayout to alignParentTop="true",

The bottom RelativeLayout to alignParentBottom="true".

The middle layout to

android:layout_below="@id/topLayout"
android:layout_above="@+id/bottomLayout" 

Problem: Scrollview does not scroll when screen is small, instead topLayout stays at top and bottomLayout stays at the bottom. The middle layout gets small and (even lost) as like below:

enter image description here

Desired: I want topLayout stay at top and bottomLayout stay at bottom. But when there is no space there must be scroll so they middle layout must not get lost.

Code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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/white"
    tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:paddingBottom="@dimen/halfClassic"
        android:paddingLeft="@dimen/halfClassic"
        android:paddingRight="@dimen/halfClassic"
        android:paddingTop="@dimen/halfClassic">

        <RelativeLayout
            android:id="@+id/layoutWeekChoice"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:padding="0dp"
            android:background="@color/white"
            android:layout_alignParentTop="true">

            <Spinner
                android:id="@+id/spinnerWeekChoice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minHeight="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/buttonSetDate"
                android:layout_toStartOf="@+id/buttonSetDate"
                />
            <Button
                android:id="@+id/buttonSetDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:maxWidth="150dp"
                android:textSize="16sp"
                android:text="Set Date"
                android:gravity="center"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:background="@drawable/button_background"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"
                android:layout_alignBottom="@+id/spinnerWeekChoice"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutCardRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center"
            android:layout_marginTop="32dp"
            android:layout_below="@id/layoutWeekChoice"
            android:layout_above="@+id/layoutShowAttendance"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageCard"
                android:src="@drawable/std_card"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="16dp"
                android:layout_centerHorizontal="true"/>

            <TextView
                android:id="@+id/textWarningCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Checking..."
                android:textColor="@color/black"
                android:textSize="14sp"
                android:layout_below="@+id/imageCard"
                android:layout_centerHorizontal="true"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutShowAttendance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/halfClassic"
            android:background="@color/white"
            android:layout_alignParentBottom="true">

            <TextView
                android:id="@+id/textAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Attended: "
                android:textColor="@color/black"
                android:layout_alignParentTop="true"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/textNotAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Not Attended: "
                android:textColor="@color/black"
                android:textSize="16sp"
                android:layout_marginBottom="32dp"
                android:layout_below="@+id/textAttended"/>

            <Button
                android:id="@+id/buttonManualAttendance"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_height="wrap_content"
                android:text="Manual Attendance"
                android:gravity="center"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:background="@drawable/button_background"
                android:minHeight="50dp"
                android:layout_below="@id/textNotAttended"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"/>

        </RelativeLayout>


    </RelativeLayout>
</ScrollView>

Note: I do not want to use LinearLayout with weight="1" which will make my 3 layouts same height

EDIT: now ScollView scrolls with new code below but my bottom TextViews get lost:

enter image description here

Code Updated:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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/white"
    tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingBottom="@dimen/halfClassic"
        android:paddingLeft="@dimen/halfClassic"
        android:paddingRight="@dimen/halfClassic"
        android:paddingTop="@dimen/halfClassic">

        <RelativeLayout
            android:id="@+id/layoutWeekChoice"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:padding="0dp"
            android:background="@color/white"
            android:layout_alignParentTop="true">

            <Spinner
                android:id="@+id/spinnerWeekChoice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minHeight="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/buttonSetDate"
                android:layout_toStartOf="@+id/buttonSetDate"
                />
            <Button
                android:id="@+id/buttonSetDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:maxWidth="150dp"
                android:textSize="16sp"
                android:text="Set Date"
                android:gravity="center"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:background="@drawable/button_background"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"
                android:layout_alignBottom="@+id/spinnerWeekChoice"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutCardRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center"
            android:layout_marginTop="32dp"
            android:layout_below="@id/layoutWeekChoice"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageCard"
                android:src="@drawable/std_card"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="16dp"
                android:layout_centerHorizontal="true"/>

            <TextView
                android:id="@+id/textWarningCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Checking..."
                android:textColor="@color/black"
                android:textSize="14sp"
                android:layout_below="@+id/imageCard"
                android:layout_centerHorizontal="true"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutShowAttendance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/halfClassic"
            android:background="@color/white"
            android:layout_below="@id/layoutCardRead"
            android:layout_alignParentBottom="true">

            <TextView
                android:id="@+id/textAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Attended: "
                android:textColor="@color/black"
                android:layout_above="@+id/textNotAttended"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/textNotAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Not Attended: "
                android:textColor="@color/black"
                android:textSize="16sp"
                android:layout_marginBottom="32dp"
                android:layout_above="@+id/buttonManualAttendance" />

            <Button
                android:id="@+id/buttonManualAttendance"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_height="wrap_content"
                android:text="Manual Attendance"
                android:gravity="center"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:background="@drawable/button_background"
                android:minHeight="50dp"
                android:layout_alignParentBottom="true"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"/>

        </RelativeLayout>


    </RelativeLayout>
</ScrollView>

Upvotes: 0

Views: 640

Answers (2)

Nitin Mali
Nitin Mali

Reputation: 216

From your 2nd relative layout,that is layoutcard remove android:layout_above="@+id/layoutShowAttendance"

and from layoutShowAttendance remove android:layout_alignParentBottom="true" and add android:layout_below="@+id/layoutCardRead" to layoutShowAttendance

It will work.

Upvotes: 1

Ircover
Ircover

Reputation: 2446

Set your parents RelativeLayout height to wrap_content. Match_parent needs only ScrollView, everything inside it can spend all the space it needs.

Upvotes: 1

Related Questions