views:

131

answers:

2

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>
A: 

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

Martin
Let me try and I'll get back to you on this.
Ragunath Jawahar
@martin, your idea is good, I just want to know are there ways to specify the width of the screen generically? (i.e) Should work on all devices with various screen sizes.
Ragunath Jawahar
+1  A: 

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?

radek-k
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
Did you try my code?
radek-k
Oops... Didn't work pal. Anyway thanks @radek-k
Ragunath Jawahar
Make sure to set `android:layout_width="fill_parent"` in `<RelativeLayout>` tag. Code is updated.
radek-k
Good job @radek-k, working code! Thanks a lot!
Ragunath Jawahar