Android EditText and Button on same line
Does it have to be Relative Layout?
I would suggest the following: set EditText
layout_width
to fill_parent
and its layout_weight
to 1, something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">
<EditText android:text="@+id/EditText01"
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent">
</EditText>
<Button android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
In that case, the width for the EditText
must be set to fill_parent
(instead of wrap_content
):
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
<Button android:id="@+id/search_text"
android:layout_toRightOf="@id/search_box"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Edit
As you said, it hides the button. Then, just invert the order:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/search_text"
android:layout_alignParentRight="true"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/search_text"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
</RelativeLayout>