10

So, I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FFFF00"
                android:minHeight="100dp"
                android:layout_gravity="bottom"
        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:background="#FF0000"
            android:text="Hello World"
            />

    <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:text="button"/>
</RelativeLayout>

and this is how it looks:

enter image description here

but if I add android:layout_alignParentBottom="true" to the button here is how it looks:

enter image description here

  1. Can someone please explain me this behavior?
  2. How to put my button at the bottom without resizing the yellow layout and without adding thousand of layouts for workarounds?
Onik
  • 19,396
  • 14
  • 68
  • 91
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196

6 Answers6

8

this solution worked for me

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FFFF00"
                android:minHeight="100dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
        >
    <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:background="#FF0000"
            android:text="Hello World"
            />

    <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="bottom|right"
            android:text="button"/>
</FrameLayout>
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
1

this one fast one screen

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:background="#FFFF00"
    android:minHeight="100dp" >



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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/button1"
        android:background="#000000"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:background="#000000"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>
Android
  • 1,417
  • 9
  • 11
1

just for try, can you double the min size of your layout and try it again? or maybe you can set a fix the hight of the layout and change it dynamically from the code when needed.

cagla
  • 538
  • 4
  • 7
0

Unfortunately bug still exists in newer versions of android.

Anyway, I found that following can solve that problem :

  1. Delete minHeight of RelativeLayout

  2. Insert TextView in a LinearLayout:

    <LinearLayout
        android:id="@+id/tvLayout"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:minHeight="100dp"
        android:gravity="top"
        >
        <TextView 
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFFF00FF"
            android:text="Helo"
            />
    </LinearLayout>
    
  3. Delete layout_alignParentBottom from Button and add layout_alignBottom="@id/tvLayout"

Now LinearLayout "controls" the height of RelativeLayout. If TextView height is bigger than minHeight of 100dp, it will expand.

And button will always align its bottom to LinearLayout, equal to RelativeLayout one.

Alexander
  • 1
  • 1
0

You can build a responsive UI with ConstraintLayout

compile 'com.android.support.constraint:constraint-layout:1.0.2'

The result layout will looks like

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#FFFF00"
    android:minHeight="100dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="Hello World"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

The preview is: enter image description here

Read more here -
https://developer.android.com/training/constraint-layout/index.html
https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:background="#FFFF00"
    android:minHeight="100dp" >

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:textColor="#000000"
            android:background="#FF0000"
            android:text="Hello World"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="button" />

</RelativeLayout>

try this code

Lal
  • 14,726
  • 4
  • 45
  • 70
Android
  • 1,417
  • 9
  • 11
  • using your code it makes my layout to cover all the screen because you've used android:layout_height="fill_parent" and I want that my layout to keep it's height as seen in my first screenshot – Buda Gavril Mar 23 '12 at 10:07
  • so I want my layout look like my first image, except that my button should be aligned to bottom. – Buda Gavril Mar 23 '12 at 10:21
  • this one you give size moer button to change – Android Mar 23 '12 at 10:25