tags:

views:

232

answers:

4
+1  Q: 

Layout Question

I have the following layout xml. I am trying to place the TextView to the left, and the Radiobutton to the right. How can I accomplish this?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </RadioButton>
</LinearLayout>

thanks

patrick

A: 

You need to add android:layout_weight="1" to your TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    </RadioButton>
</LinearLayout>
CaseyB
this did not work either. I thought I had alredy tried this.
I copied the code you posted into a new xml file and loaded it into the layout editor, added the weight=1 and it works for me. I've used this several times in my layouts.
CaseyB
+5  A: 

Forget LinearLayout, use RelativeLayout. This should put the TextBox on the left and the RadioButton on the right like you ask.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/txtVehName">
    </RadioButton>
</RelativeLayout>

Eclipsed4utoo is 100% correct, you should be using dp instead of sp because they will render visually the same size across all devices.

Dinedal
`RelativeLayout` is what you should use when trying to get Views to show up beside each other. The `LinearLayout` has them above/below each other. Use the `layout_margin` attributes to move the Views around. Also use `dip` instead of pixels.
Eclipsed4utoo
Still not quit right. First, the value of toLeftOf will not compile. I tried using a value of "@+id/txtVehName". Also since I want the RadioButton on the Right, shouldn't I use toRightOf. I tried both, but the RadioButton did not show up either way.
You don't want the + there, that will make a new ID, you want to reference the old one. The typo about the value of layout_toLeftOf has been fixed above.
Dinedal
+2  A: 

This should place the txtVehName against the left wall, and the rbDefault against the right wall. Is that what you were trying to accomplish?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TextView
      android:id="@+id/txtVehName"
      android:hint="@string/VEH_NAME"
      android:textSize="18sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentBottom="true">
    </TextView>
    <RadioButton
      android:id="@+id/rbDefault"
      android:text=""
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true">
    </RadioButton>
</RelativeLayout>
Mayra
thanks for your suggestion. This is preety much what I did, however I had to use marginTop="10dip" so the textviewwould display in the middle. Using android:layout_alignParentBottom="true" still had the textview at the top.
A: 

Thanks everyone for your answers.

CaseyB - not sure why your solution won't work for me. here is the displayed output from your suggestion:

alt text

Here is the xml that seems to work for me.

<RelativeLayout  
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
  <TextView 
      android:id="@+id/txtVehName" 
      android:hint="@string/VEH_NAME" 
      android:textSize="18dp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_marginTop="10dip"
      > 
    </TextView> 
    <RadioButton 
      android:id="@+id/rbDefault" 
      android:text="" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      > 
    </RadioButton> 
</RelativeLayout>

here is the emulater display output

alt text

Eclipsed4utoo - dp instead of sp...thanks