jcm
jcm

Reputation: 1789

Aligning an EditText with a Button

I am trying to horizontally align an EditText with a Button without success. Both should be of the same height and their tops and bottoms should align with each other. Here's my code:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/quantity"
        android:inputType="number"
        android:layout_height="fill_parent"
        android:layout_width="100dp"
        android:layout_toRightOf="@id/quantity_label"
        android:background="@android:drawable/editbox_background" />
    <Button
        android:onClick="submitButtonTapped"
        android:id="@+id/submit"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/quantity"
        android:drawableLeft="@drawable/submit_icon"
        android:drawablePadding="5dp"
        android:text="@string/submit" />
</LinearLayout>

The result is that the bottom of the two elements are aligned. The focus border of the EditText extends below the Button. The EditText does not extend to the height of the Button, even with the focus border. How do I get them aligned?

Upvotes: 1

Views: 2647

Answers (2)

Chirag Patel
Chirag Patel

Reputation: 11508

just add margin to button

like this

android:layout_marginTop="5dp"

also use RelativeLayout instead of LinearLayout and also use

andorid:layout_alignTop = "@id/quantity"

hope this will work..

Upvotes: 0

Report Error
Report Error

Reputation: 414

  1. Set in dimen/line_height value.
  2. Use this layout
<?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="@dimen/line_height"
android:orientation="horizontal" >
<EditText
    android:id="@+id/quantity"
    android:inputType="number"
    android:layout_height="fill_parent"
    android:layout_width="100dp"
    android:background="@android:drawable/editbox_background" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:drawableLeft="@drawable/submit_icon"
    android:onClick="submitButtonTapped"
    android:text="@string/submit" />
 </LinearLayout>

Upvotes: 1

Related Questions