Search code examples
androidandroid-scrollviewandroid-relativelayout

RelativeLayout Buttons disappear when ScrollView used


I have a layout similar to the on used in this example

enter image description here

The only difference is that I have 3 buttons that I wanted to add following the ScrollView. Unfortunately as soon as I added the ScrollView, my buttons disappeared off the screen. (When I had a TextView without the ScrollView it was fine).

How can I get my buttons back at the bottom of the screen?

<?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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_heading"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:textStyle="bold"
        android:text="@string/article_title"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_subheading"
        android:layout_below="@id/article_heading"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault"
        android:text="@string/article_subtitle"/>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/article_scrollview"
        android:layout_below="@id/article_subheading">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="@dimen/line_spacing"
            android:id="@+id/article"
            android:padding="@dimen/padding_regular"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault"
            android:autoLink="web"
            android:text="@string/article_text"/>

    </ScrollView>

    <Button
        android:id="@+id/butConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/article_scrollview"
        android:layout_marginStart="@dimen/button_margin"
        android:onClick="onButLoginClicked"
        android:text="@string/but_text_connect"/>

    <Button
        android:id="@+id/butAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/butConnect"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_add" />

    <Button
        android:id="@+id/butSale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/butAdd"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_sale" />

</RelativeLayout>

Solution

  • Design your layout carefully and follow design principle follow https://developer.android.com/design

    <?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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_heading"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:textStyle="bold"
        android:text="@string/article_title"/>
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/article_subheading"
        android:layout_below="@id/article_heading"
        android:padding="@dimen/padding_regular"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault"
        android:text="@string/article_subtitle"/>
    
    <ScrollView
        android:layout_above="@+id/butConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/article_scrollview"
        android:layout_below="@id/article_subheading">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="@dimen/line_spacing"
            android:id="@+id/article"
            android:padding="@dimen/padding_regular"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault"
            android:autoLink="web"
            android:text="@string/article_text"/>
    
    </ScrollView>
    
    <Button
        android:id="@+id/butConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/butAdd"
        android:layout_marginStart="@dimen/button_margin"
        android:onClick="onButLoginClicked"
        android:text="@string/but_text_connect"/>
    
    <Button
        android:id="@+id/butAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/butSale"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_add" />
    
    <Button
        android:id="@+id/butSale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/button_margin"
        android:text="@string/but_text_sale" />
    
    </RelativeLayout>