1

I want to implement a screen layout as shown below, with a Scrollview at the top of the screen, and a block of 2 buttons which are fixed at the bottom of the screen. I also want the buttons to occupy a fixed proportion of the screen width, and have used weights in a horizontal layout to achieve this. Desired layout

The ScrollView rows are populated from a database table, and may contain too many rows to be displayed on a single screen. For this reason I only want the ScrollView to occupy a fixed proportion of the top of the screen, so that the buttons always appear at the bottom of the screen.

I tried adapting the solution here: Android ScrollView and buttons at bottom of the screen - substituting my own interface elements, but this resulted in the ScrollView occupying the whole screen, and the buttons not being visible at all. I suspect this is because the weights in my "layoutContainer" LinearLayout were conflicting with the design of the suggested solution.

My current code is below. This renders the ScrollView and Buttons fine until the Scrollview gets too large - when that happens the buttons disappear from the bottom of the screen.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context="com.itm.timer.ItemActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="top"
        android:text="@string/activity_meal_item_textview1"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/scrollViewRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="75" >

        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/layoutContainer"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="2" >
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="20" >

            <Button
                android:id="@+id/btnMealPlan"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/clipboard"
                android:onClick="onClickMealPlan"
                android:text="Show Meal Plan" />

            <Button
                android:id="@+id/btnStartTimer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnMealPlan"
                android:drawableLeft="@drawable/starttimer"
                android:onClick="onClickStartTimer"
                android:text="Set Reminders" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="2" >
        </RelativeLayout>
    </LinearLayout>


</LinearLayout>

Any ideas for how to achieve what I want, or how to simplify my design to achieve the aim of accommodating the ScrollView and Buttons in fixed positions on the screen?

Community
  • 1
  • 1
Ian M
  • 567
  • 8
  • 33

3 Answers3

1

try like this it may help you,

<LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:padding="5dp"
        android:text="MeatItem"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/scrollViewRecords"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light" >

        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="ScoollView"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/layoutContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_dark"
            android:text="Button1"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@android:color/holo_blue_dark"
            android:text="Button2"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@android:color/holo_blue_dark"
            android:text="Button3"
            android:textColor="@android:color/white" />
    </LinearLayout>

</LinearLayout>
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context="com.itm.timer.ItemActivity"
    tools:ignore="MergeRootFrame"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:text="@string/activity_meal_item_textview1"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/scrollViewRecords"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/layoutContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="0dp"
            android:layout_height="1"
            android:layout_weight="0.10"/>

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.80" 
            android:orientation="vertical">

            <Button
                android:id="@+id/btnMealPlan"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/clipboard"
                android:onClick="onClickMealPlan"
                android:text="Show Meal Plan" />

            <Button
                android:id="@+id/btnStartTimer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/starttimer"
                android:onClick="onClickStartTimer"
                android:text="Set Reminders" />

            <Button
                android:id="@+id/btnStartTimer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/starttimer"
                android:onClick="onClickStartTimer"
                android:text="Set Reminders" />
        </LinearLayout>

        <View
            android:layout_width="0dp"
            android:layout_height="1"
            android:layout_weight="0.10"/>
    </LinearLayout>
</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

Just use layout_height="0dp" and layout_weight="1" in your scrollview.

Vinayak Bhavnani
  • 618
  • 1
  • 4
  • 10