Andrew
Andrew

Reputation: 3706

How to set up a layout for EditText and Button aligned by the topline?

I'm trying to create a kind of "Buttoned Edit" control, when EditText and Button controls are placed in the same line. I used a simple LinearLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editTextFileName"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Database file name"
        android:inputType="textUri" />

    <Button
        android:id="@+id/buttonFileSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="46dip"
        android:text="..." />

</LinearLayout>

The problem is edit box and button are not aligned on the same "topline": layout topline spacing

A closer look: enter image description here

On a different emulators I'm getting a different spacing.

How to make both EditText and Button to be aligned on a topline always?

SOLUTION (thanks to @steevoo and @Mohamed_AbdAllah):

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/buttonFileSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="46dip"
        android:layout_alignParentRight="true"
        android:text="..." />

    <EditText
        android:id="@+id/editTextFileName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_toLeftOf="@id/buttonFileSelect"
        android:layout_alignTop="@id/buttonFileSelect"
        android:hint="Database file name"
        android:inputType="textUri" />

</RelativeLayout>

Upvotes: 2

Views: 707

Answers (1)

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

Try using a RelativeLayout instead and use android:layout_alignTop to align the tops of the Button and the EditText. Also, you can give them the same fixed height (46dip) but this can cause text to be clipped in the EditText

Upvotes: 2

Related Questions