How to set this XML Android layout so that the text scrolls but the buttons still show?

laurie Source

I have the following layout consisting of several text boxes which I want to scroll as one object. I then have a linear layout below that which contains navigation buttons - I want this to stick to the bottom of the screen and not scroll.

Right now the bottom linear layout is pushed off the screen - I suspect because of the setting of the height of the scroll view. However I can't see the trick to resolve this. How can I make it so that the buttons in the linear view are always visible and stick to the bottom of the screen (not the bottom of the scrollview)?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    tools:context="com.QuestionView">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <ScrollView
        android:id="@+id/ScrollView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/questionText"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scrollbars="vertical"
                android:text="TextView"
                android:textSize="24dp" />

            <TextView
                android:id="@+id/answerA"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scrollbars="vertical"
                android:text="TextView"
                android:textSize="24dp" />

            <TextView
                android:id="@+id/answerB"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scrollbars="vertical"
                android:text="TextView"
                android:textSize="24dp" />

            <TextView
                android:id="@+id/answerC"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scrollbars="vertical"
                android:text="TextView"
                android:textSize="24dp" />

            <TextView
                android:id="@+id/answerD"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:scrollbars="vertical"
                android:text="TextView"
                android:textSize="24dp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#ff9933"
            android:text="Back" />

        <Button
            android:id="@+id/flip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#1a75ff"
            android:text="Flip" />

        <Button
            android:id="@+id/next"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#ff9933"
            android:text="Next" />
    </LinearLayout>
</LinearLayout>
androidxmllayout

Answers

answered 8 months ago Gabe Sechan #1

Use a RelativeLayout for the outer layout. Define the Linear layout with buttons to be layout_alignParentBottom. Make the ScrollView layout_above the button layout and layout_below the toolbar (which should be alignParentTop). Then the buttons will be on the bottom, the toolbar on top, and the scroll view between them.

answered 8 months ago yatin deokar #2

Best way to do this is to use android.support.design.widget.CoordinatorLayout to get Button Layout at bottom use android:layout_gravity="bottom" like below

Step 1:

Add compile 'com.android.support:design:26.1.0'

step 2:

 <android.support.design.widget.CoordinatorLayout 
    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"
    tools:context="com.QuestionView">

<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">

        <TextView
            android:id="@+id/questionText"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:text="TextView"
            android:textSize="24dp" />

        <TextView
            android:id="@+id/answerA"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:text="TextView"
            android:textSize="24dp" />

        <TextView
            android:id="@+id/answerB"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:text="TextView"
            android:textSize="24dp" />

        <TextView
            android:id="@+id/answerC"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:text="TextView"
            android:textSize="24dp" />

        <TextView
            android:id="@+id/answerD"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical"
            android:text="TextView"
            android:textSize="24dp" />

    </LinearLayout>

</ScrollView>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

    <Button
        android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="#ff9933"
        android:text="Back" />

    <Button
        android:id="@+id/flip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="#1a75ff"
        android:text="Flip" />

    <Button
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="#ff9933"
        android:text="Next" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>

comments powered by Disqus