Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making EditText and Button same height in Android

I have an EditText and a Button in my LinearLayout and I want to align them closely together so they see seem to belong together (edittext + micButton for speech input).

Now they don't have the same height and they aren't really aligned well (Button seems to be a little lower than the EditText). I know I can apply a negative margin like -5dp to make them come closer together, but is there perhaps a better way to do this?

Set them in a specific container/layout so that they will automatically have the same height and no margin between them?

like image 955
Daniel Avatar asked Aug 18 '10 20:08

Daniel


3 Answers

Using relative layout you can stretch a view depending upon another views size without knowing the exact size of the other view. Here is the code :

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<Button 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="button"
    android:id="@+id/but"/>
<EditText 
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/but"
    android:layout_alignBottom="@id/but"
    android:layout_alignTop="@id/but"
    />
</RelativeLayout>

Check this link for reducing space between views : https://groups.google.com/forum/?fromgroups#!topic/android-developers/RNfAxbqbTIk

like image 77
Muthumani Avatar answered Nov 16 '22 05:11

Muthumani


Hmm, don't know why people bother so much with tables. Since the both Views are within a LinearLayout (presumable orientation=Horizontal), this command should center both within the layout:

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

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />

    </LinearLayout>

Note: Since EditTexts and Buttons may orient their text slightly differently, you may have to do some tweaking (by changing margins or padding) to get the text to align properly.

like image 1
SMBiggs Avatar answered Nov 16 '22 05:11

SMBiggs


I hope this solution might help for your scenario...Here is the code..

    <RelativeLayout
        android:id="@+id/rlLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal"
        android:padding="3dp" >

    <EditText
        android:id="@+id/etId"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="#c8c8c8"
        android:hint="Edittext"
        android:paddingLeft="20dip"
        android:paddingRight="10dip"
        android:textColor="#000000" />

    <RelativeLayout
        android:id="@+id/rlLayoutid"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignRight="@+id/etId" >

        <Button
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="14dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/calender" />
    </RelativeLayout>
</RelativeLayout>

like image 1
Rajakumar Avatar answered Nov 16 '22 05:11

Rajakumar