About design and nearby

How to Align Android EditText and Button on the Same Line

Posted: June 30th, 2015 | Author: | Filed under: Mobile Design and Development | Tags: , , , , , | No Comments »

Sometimes even such easy question as aligning an EditText beside a button could be tricky. The EditText should fill as much of the width as possible and the button should be at the right hand side and does not take additional space at the top/ bottom:

How to align EditText and Button

The solution for me is to use LinearLayout to display the elements in the order they were added and set EditText layout_width to fill_parent and its layout_weight to 1 (100% of width, the button covers the rest):

  1. <LinearLayout
  2.             android:layout_height="wrap_content"
  3.             android:layout_width="fill_parent">    
  4.         <EditText
  5.             android:layout_height="wrap_content"
  6.             android:layout_weight="1"
  7.             android:layout_width="fill_parent"
  8.             android:hint="@string/email_hint"
  9.             android:id="@+id/email_hint"
  10.             android:inputType="textEmailAddress"
  11.             android:layout_toLeftOf="@+id/do_email_picker"
  12.             android:layout_alignParentLeft="true"
  13.             android:textColor="@color/grey" />
  14.              <Button
  15.               style="?android:buttonBarButtonStyle"
  16.               android:id="@+id/do_email_picker"
  17.               android:layout_width="wrap_content"
  18.               android:layout_height="wrap_content"
  19.               android:layout_weight="0"
  20.               android:text="@string/icon_add"
  21.               android:layout_alignParentRight="true"
  22.               android:onClick="doLaunchContactPicker"
  23.               android:textSize="26sp"
  24.               android:textColor="@color/blue" />        
  25.       </LinearLayout>
Share Button

Check Related Posts:


Leave a Reply


  • × 2 = eighteen

Looking for a Freelance UX & UI designer for your project? I’m ready to jump onboard!

Let's discuss your project now