Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RelativeLayout : how to alignParentBottom when wrapped in a ScrollView?

Tags:

android

The problem I am having seems to be that if I have a view (e.g. myButton) inside a RelativeLayout set to alignParentBottom - (as per the code below) - and then wrap the RelativeLayout with a scrollview (necessary so the content will be viewable on smaller screens or when orientation changes to landscape) - then, when the parent bottom is NOT visible (for example when changed to landscape orientation) myButton is displayed at the TOP of the page - NOT the bottom.

How can you align a view to the bottom of the screen and have it remain at the bottom even if the bottom is below the scroll?

<ScrollView android:id="@+id/mainScrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:fillViewport="true">    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/topLayout"  android:layout_width="fill_parent"  android:layout_height="fill_parent">    ... lots of views and stuff   <Button android:layout_alignParentBottom="true"  android:id="@+id/myButton"   android:text="Click Me" />  </RelativeLayout> </ScrollView>    
like image 689
JohnRock Avatar asked Jun 27 '10 03:06

JohnRock


2 Answers

Take the button out of the SCrollview, Wrap both the Scrollview and the Button in a Linear Layout, set the scrollview's height to 0dip and set it's weight to 1, do not set any weight property for the button, so the scrollview can occupy all the remaining space saving only the button's space way at the bottom.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <ScrollView          android:layout_width="match_parent"         android:layout_height="0dip"         android:layout_weight="1">         <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="horizontal" >          ....lots of views...           </LinearLayout>     </ScrollView>      <Button         android:id="@+id/button1"         android:layout_width="match_parent"         android:layout_height="40dp"         android:text="Button" />  </LinearLayout> 
like image 54
kunmi Avatar answered Sep 19 '22 06:09

kunmi


Using fill_parent as the height of a child of a ScrollView is meaningless. You are telling the RelativeLayout to be always as tall as its parent. In this case, the ScrollView becomes useless! The height of the RelativeLayout should be set to wrap_content, in which case, depending on what the RelativeLayout contains, alignParentBottom may not work as you expect. You should simply use a LinearLayout, it will be much much simpler.

like image 30
Romain Guy Avatar answered Sep 21 '22 06:09

Romain Guy