How to put EditText and Button next to each other?

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

Here 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 adjusting it using the width of the EditText with the exact amount of dp, but then when the screen rotates horizontally, there is a big gap between the EditText and Button.

+7
source share
3 answers

If you want to continue using the relative layout and change the edit text until the button is added to edittext xml

 android:layout_toLeftOf="@+id/skipButton" 
+19
source

Instead of using RelativeLayout, just change LinearLayout to have android: orientation = "horizontal". Then all glances inside it will be inserted from the side. Remove alignParent objects (they are for relative layouts) and set editText (which I think 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> 
+13
source
  <LinearLayout android:id="@+id/measure" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/product_description" android:layout_margin="10dp" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:background="#ff669900" android:orientation="horizontal" android:padding="1px" android:textColor="#fff" android:weightSum="2" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:freezesText="true" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:padding="5dp" android:scrollHorizontally="true" android:singleLine="true" android:text="IMEI" android:textColor="#fff" android:textSize="15sp" android:textStyle="bold" /> <EditText android:id="@+id/connection_type" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:background="@color/white" android:gravity="center" android:hint="" android:padding="10dp" android:textColor="#ff669900" android:textSize="15sp" /> </LinearLayout> 

Out Put looks like this: -

EditText next to TextView

Perhaps this will help, you can change the TextView to a button or change the position, it all depends on you.

+1
source

All Articles