7

Pictures: http://img838.imageshack.us/img838/1402/picse.png

How do I make the layout in Pic. 2 using RelativeLayout ONLY. Below is the xml layout of the first screenshot.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
<TextView
    android:id="@+id/timer_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="10dip"
    android:textSize="30sp"
    android:layout_alignParentTop="true"
    android:text="@string/timer_start" />

<ListView 
    android:id="@+id/questions_listview"
    android:layout_below="@id/timer_textview"  
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />

<Button android:id="@+id/true_btn"
    android:text="@string/true_option"
    android:layout_alignParentBottom="true"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/false_btn"
    android:text="@string/false_option"
    android:layout_toRightOf="@id/true_btn"
    android:layout_alignBaseline="@id/true_btn"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
  • 2
    `android:layout_weight` has no meaning in a `RelativeLayout`. I am not aware of a way to implement what you seek using a single `RelativeLayout`, sorry. – CommonsWare Aug 24 '10 at 11:57

2 Answers2

18

You could use LinearLayout at bottom to achieve this. But you want to use RelativeLayout layout only so:

<RelativeLayout android:layout_width="fill_parent" ... >

...    

<View android:id="@+id/helper" 
android:layout_width="0dp"
android:layout_height="0dp" 
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

<Button android:id="@+id/true_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_toLeftOf="@id/helper"/>

<Button
android:id="@+id/false_btn"
android:layout_alignBaseline="@id/true_btn"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_toRightOf="@id/helper" />

</RelativeLayout>

Can't you really use nested layouts?

plugmind
  • 7,926
  • 4
  • 34
  • 39
  • I know I can use a nested layout, in the second screenshot I did exactly that. But I just want to know the limitations of RelativeLayout. – Ragunath Jawahar Aug 24 '10 at 14:12
  • Make sure to set `android:layout_width="fill_parent"` in `` tag. Code is updated. – plugmind Aug 29 '10 at 12:00
  • What's interesting here is that even though the width is designated as `"wrap_content"` for the button, its width is adjusted to fill in the space between the blank view (which is in the center) and the parent edge since `android:layout_alignParentRight="true"` and `android:layout_toRightOf="@+id/helper"` are set. Neato, thanks! – rcl Jun 02 '11 at 01:21
  • Bravo -- exactly what i needed. GJ. – adjfac Jun 30 '11 at 15:58
0

If you change the buttons to

<Button android:id="@+id/true_btn"
    android:text="@string/true_option"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/false_btn"
    android:text="@string/false_option"
    android:layout_toRightOf="@id/true_btn"
    android:layout_alignBaseline="@id/true_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

you use at least the full width although the false_btn gets all remaining space.

To increase the width of the first true_btn you could set the minWidth attribute - either use a fixed value or get the screen width and make the minWidth the half of it

DonGru
  • 13,532
  • 8
  • 45
  • 55