Search code examples
androidxmlscrollviewandroid-linearlayoutandroid-scrollview

ScrollView takes away LinearLayout constraints


I'm trying to implement a ScrollView on a login screen over a LinearLayout, but the scroll view seems to mess up all the constraints on the LinearLayout.

Whats supposed to look like this

enter image description here

Instead looks like this when ScrollView is added

enter image description here

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_root_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bitmap_resource"
tools:context="dreamentries.dreamentries.LoginActivity">

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

    <LinearLayout
        android:id="@+id/linear_center_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="10dp"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="text"
            android:textSize="30dp"
            android:visibility="invisible" />

        <EditText
            android:id="@+id/login_edittxt_email"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="25dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginStart="25dp"
            android:alpha="0.95"
            android:background="#202020"
            android:hint="EMAIL"
            android:inputType="textEmailAddress"
            android:letterSpacing="0.095"
            android:padding="5dp"
            android:paddingEnd="15dp"
            android:paddingLeft="15dp"
            android:paddingStart="15dp"
            android:textColor="#fff"
            android:textColorHint="#fff"
            android:textSize="15dp" />

        <EditText
            android:id="@+id/login_edittxt_pass"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginEnd="25dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginStart="25dp"
            android:layout_marginTop="5dp"
            android:alpha="0.95"
            android:background="#202020"
            android:hint="PASSWORD"
            android:inputType="textPassword"
            android:letterSpacing="0.095"
            android:padding="5dp"
            android:paddingEnd="15dp"
            android:paddingLeft="15dp"
            android:paddingStart="15dp"
            android:textColor="#fff"
            android:textColorHint="#fff"
            android:textSize="15dp" />

    </LinearLayout>
</ScrollView>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linear_center_login"
    android:layout_marginEnd="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="2dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="text"
        android:textSize="40dp"
        android:visibility="invisible" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="25dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginStart="25dp"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/btn_login_earlyadopter"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:layout_marginEnd="4dp"
            android:layout_marginRight="4dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#C0202020"
            android:gravity="center"
            android:letterSpacing="0.095"
            android:text="REGISTER"
            android:textColor="#fff"
            android:textSize="17dp" />

        <TextView
            android:id="@+id/btn_login_login"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#C0202020"
            android:gravity="center"
            android:letterSpacing="0.095"
            android:text="LOGIN"
            android:textColor="#fff"
            android:textSize="17dp" />

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

What am I missing in the ScrollView Attribute that prevents it from messing up the LinearLayout


Solution

  • I had to wrap the linearLayouts in a RelativeLayout then set the Scrollview to:

    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    

    The following will achieve the effect you are looking for:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_root_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bitmap_resource"
    tools:context="dreamentries.dreamentries.LoginActivity">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <LinearLayout
            android:id="@+id/linear_center_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:orientation="vertical"
            android:padding="5dp">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="text"
                android:textSize="30dp"
                android:visibility="invisible" />
    
            <EditText
                android:id="@+id/login_edittxt_email"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginStart="25dp"
                android:alpha="0.95"
                android:background="#202020"
                android:hint="EMAIL"
                android:inputType="textEmailAddress"
                android:letterSpacing="0.095"
                android:padding="5dp"
                android:paddingEnd="15dp"
                android:paddingLeft="15dp"
                android:paddingStart="15dp"
                android:textColor="#fff"
                android:textColorHint="#fff"
                android:textSize="15dp" />
    
            <EditText
                android:id="@+id/login_edittxt_pass"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginEnd="25dp"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginStart="25dp"
                android:layout_marginTop="5dp"
                android:alpha="0.95"
                android:background="#202020"
                android:hint="PASSWORD"
                android:inputType="textPassword"
                android:letterSpacing="0.095"
                android:padding="5dp"
                android:paddingEnd="15dp"
                android:paddingLeft="15dp"
                android:paddingStart="15dp"
                android:textColor="#fff"
                android:textColorHint="#fff"
                android:textSize="15dp" />
    
        </LinearLayout>
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linear_center_login"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="2dp"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="text"
            android:textSize="40dp"
            android:visibility="invisible" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="25dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginStart="25dp"
            android:orientation="horizontal">
    
    
            <TextView
                android:id="@+id/btn_login_earlyadopter"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginEnd="4dp"
                android:layout_marginRight="4dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="#C0202020"
                android:gravity="center"
                android:letterSpacing="0.095"
                android:text="REGISTER"
                android:textColor="#fff"
                android:textSize="17dp" />
    
            <TextView
                android:id="@+id/btn_login_login"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginLeft="4dp"
                android:layout_marginStart="4dp"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="#C0202020"
                android:gravity="center"
                android:letterSpacing="0.095"
                android:text="LOGIN"
                android:textColor="#fff"
                android:textSize="17dp" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    </RelativeLayout>
    
    </ScrollView>