kishoredbn
kishoredbn

Reputation: 2087

How to keep Android Button view in the bottom of the screen?

I have an activity which contains mainly 2 components: A Scroll view and a Button. I want to place the button at the buttom of the screen, fixed.

I tried many things but i cannot keep the button location fixed at the button of the screen, bacause as the content of the scroll view increases, the button is pushed outside the activity screen.

I want to achieve something like this: enter image description here

Can anyone tell me how to achieve this?

My XML file is clean (all custom views are programmatically written, can't be shared!):

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_gravity="center_horizontal">

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

</ScrollView>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Save Changes"
    android:id="@+id/customButton" />

Upvotes: 2

Views: 166

Answers (4)

Raghavendra
Raghavendra

Reputation: 2303

If you use a relative layout try this

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testproject.MainActivity" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" 
    android:layout_above="@+id/button1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

</RelativeLayout>

Upvotes: 0

Ravi Makvana
Ravi Makvana

Reputation: 2902

you can use LinearLayout or relativeLayout like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_gravity="center_horizontal">

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

   </ScrollView>

   <Button
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Save Changes"
      android:id="@+id/customButton" />

</LinearLayout>

Upvotes: 0

Zsolt Mester
Zsolt Mester

Reputation: 1053

Use a LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

        <!-- ... -->

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Changes"/>

</LinearLayout>

Upvotes: 2

Larry Schiefer
Larry Schiefer

Reputation: 15775

Use a RelativeLayout to host your views and pin the Button to the bottom of the layout.

Upvotes: 0

Related Questions