0

I'm trying to put, in the same row, an ImageButton (right align, fixed width) and an EditText (left align, taken up all the remaining space), but I can't.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:text="EditText"
    android:textSize="18sp" />
<ImageButton
    android:layout_gravity="center_vertical"
    android:layout_width="40dp"
    android:layout_height="40dp" />
</LinearLayout>

I can't use the property "layout_weight" because I want to fix the width of the ImageButton. If I use "layout_width=fill_parent" in the EditText, the ImageButton disappears.

Could you help me?

Thanks!!!

Renjith
  • 5,783
  • 9
  • 31
  • 42
user2331427
  • 13
  • 1
  • 2

3 Answers3

2

If you can't use layout_weight, use RelativeLayout. In the RelativeLayout first put the ImageButton and align it to right of RelativeLayout. Then put the EditText with layout_width="fill_parent" place it to left of ImageButton

Try this

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

    <ImageButton 
        android:layout_width="40dp" 
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:id="@+id/id_image_button"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_toLeftOf="@id/id_image_button"/>

</RelativeLayout>
Renjith
  • 5,783
  • 9
  • 31
  • 42
0

Can you try the following?:

<EditText
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="EditText"
android:textSize="18sp"
android:layout_weight="1" />
Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
  • Thank you very much for your answer. Yes, the `android:layout_weight="1" />` works perfectly. I misunderstood the use of the layout_weight property. I thought that you have to specify this property for all the views in the same layout, but now I know that you can use it only in the views you want to affect. Thanks again!! – user2331427 Apr 30 '13 at 08:20
0
<?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="50dp"
    android:orientation="horizontal" >
<EditText
    android:layout_gravity="center_vertical"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="40dp"
    android:text="EditText"
    android:textSize="18sp" />
<ImageButton
    android:layout_gravity="center_vertical"
    android:layout_width="40dp"
    android:layout_height="40dp" />
</LinearLayout>

Just i change layout_width of EditText fill_parent to 0dp and set layout_weight="1", so it will stretched and take remaining space.

Check this for LinearLayout weight concept.

Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
  • Thank you very much for your answer. Yes, the `android:layout_weight="1" />` works perfectly. I misunderstood the use of the layout_weight property. I thought that you have to specify this property for all the views in the same layout, but now I know that you can use it only in the views you want to affect. Thanks again!! – user2331427 Apr 30 '13 at 08:21