Elad Benda Elad Benda - 2 months ago 12
Android Question

how to set a button at the bottom of a screen when the parent is a scroll view?

I have a scrollView (this is a solution to support landscape computability)

and in it one child- linearLayout.

I want to pose a button at the bottom of a screen.

layout_height = match_parent
is problematic as the scroll can be infinite.

How can I do this, to be compatible to all screen resolution?

this is my xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue_bg" >

<LinearLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<com.m.view.text.mTextView
android:id="@+id/verifyHeaderText"
style="@style/textOnBg"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginTop="50dp"
android:text="ENTER VERIFICATION CODE"
android:textAllCaps="true"
android:textColor="@android:color/white"
app:font_type="varela" />


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:paddingBottom="5dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="5dp" >

<LinearLayout
android:id="@+id/inputBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:background="@drawable/input_box_idle"
android:orientation="horizontal" >

<EditText
android:id="@+id/verificationCodeEditText"
style="@style/textOnBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:hint="- - - - -"
android:inputType="number"
android:maxLength="5"
android:textColor="#00bcfe"
android:textSize="25dp"
android:textStyle="italic" >
</EditText>
</LinearLayout>
</LinearLayout>

<Button
android:id="@+id/continueButton"
style="@style/textOnBg"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="@drawable/btn_selector"
android:text="Continue"
android:textColor="#00bcfe"
android:textSize="16dp"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
</ScrollView>


this is the result on wide-screen

enter image description here
The button is in the bottom of the scroll which spans 3/4 of the whole screen

Answer

Add android:fillViewport="true" to your scrollview:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.m"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_bg"
    android:fillViewport="true" >