Search code examples
androidandroid-layoutright-align

ImageButton (right-aligned, fixed width) and EditText (left-aligned, remaining space width)


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!!!


Solution

  • <?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.