views:

57

answers:

1

Given a horizontal LinearLayout in Android, I'd like to create:

(1) a textbox which fills nearly the width of the screen, except leaving enough space for...

(2) a button on the right

So I create an EditText and a Button and they're both very small.

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
 <EditText android:text="" 
   android:id="@+id/Text01"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content">
 </EditText>
 <Button android:text="+"
   android:id="@+id/Button01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="right">
 </Button>
 </LinearLayout>

I can't set the EditText to android:layout_width="fill_parent" otherwise it would obliterate the button. So how to make the EditText expand as much as it can?

+1  A: 
<?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">
 <EditText android:text="" 
   android:id="@+id/Text01"
   android:layout_height="wrap_content"
    android:layout_width="0dip"
   android:layout_weight="1">
 </EditText>
 <Button android:text="+"
   android:id="@+id/Button01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="right">
 </Button>
 </LinearLayout>
mbaird
"Error: integer types not allowed (at 'layout_width' with value '0')"
OJW
so change it to 0dip
mbaird
that gives only a very small textbox on the left, with the button next to it on the left
OJW
Do you have more to your layout than what you showed in your original question? Because the answer I gave gives me exactly what you are asking for. I edited my answer above to have the entire contents of the layout file. I just tested that in Eclipse and it gives exactly what it sounds like you are looking for. But if this is just part of a bigger layout file, then there may be some other things you need to do to the other elements in your layout to get everything to work.
mbaird
yes this is within a vertical linear layout, which is fill_parent and fills the whole screen-width. will post its xml later
OJW
oops, you were right - there was a top-level layout that didn't have fill_parent. sorry about that!
OJW