Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an EditText and Button next to each other?

I want to try to get an EditText and Button next to each other. I currently have where the button is on the right and the edit text is left aligned but the button then shows that it is on top of the EditText. I want the Button to start where the EditText ends.

This is what I have right now:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#004D79">
        <RelativeLayout android:id="@+id/relativeLayout1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content">
        <EditText 
            android:layout_alignParentLeft="true" 
            android:text="EditText"
            android:layout_marginTop="10dp" 
            android:id="@+id/editText" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent">
        </EditText>
        <Button android:text="Skip"
         android:id="@+id/skipButton" 
         android:textSize="18sp"
         android:layout_marginTop="10dp" 
         android:layout_alignParentRight="true" 
         android:layout_height="wrap_content" 
         android:layout_width="80dp">
         </Button>
    </RelativeLayout> 
</LinearLayout>

I tried to set it up with making the EditText width with an exact dp amount but then when the screen flips horizontal there is a big gap between the EditText and Button.

like image 222
Kevin Avatar asked Jun 08 '11 02:06

Kevin


2 Answers

If you want to continue to use a relative layout and have the edit text to go up until the button you can add this to the edittext xml

android:layout_toLeftOf="@+id/skipButton"
like image 197
byte me Avatar answered Nov 01 '22 11:11

byte me


Instead of using the RelativeLayout, just change your LinearLayout to have android:orientation="horizontal". Then all the views inside it will be put side to side. Remove the alignParent things (those are for relative layouts), and set the editText (which I believe you want to stretch and fill the screen) to have layout_weight="1".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#004D79"
    >
        <EditText 
            android:layout_alignParentLeft="true" 
            android:text="EditText"
            android:layout_weight="1" 
            android:id="@+id/editText" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent">
        </EditText>
        <Button android:text="Skip"
         android:id="@+id/skipButton" 
         android:textSize="18dp"
         android:layout_marginTop="10dp" 
         android:layout_height="wrap_content" 
         android:layout_width="80dp">
         </Button>
</LinearLayout>
like image 26
Christopher Souvey Avatar answered Nov 01 '22 09:11

Christopher Souvey