Android - How to align editText and submit button at bottom?

I've read several articles and several layout guides but haven't figured out how it works yet ... I have the following code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit_text"
    android:hint="Type your message" ></EditText>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Send"></Button>

<WebView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".5"
    android:id="@+id/webview" ></WebView>
</LinearLayout>

      

I would like to align my editText button and submit at the bottom and my webview above .. any ideas how can I do this?

+3


source to share


7 replies


Try this



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </WebView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Type your message" >
        </EditText>

        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Send" >
        </Button>
    </LinearLayout>

</RelativeLayout>

      

+3


source


There are many ways to make it work:



  • Linear layout

    • Display items in the order in which you added them.

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">
      
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"></WebView>
      
       <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
      
       <EditText
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Type your message"></EditText>
      
        <Button
           android:id="@+id/button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Send"></Button>
      
        </LinearLayout>
      
      
      </LinearLayout>           
      
            

      enter image description here

  • Relative layout

    • Display elements in relation to other components

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
      
       <WebView
           android:layout_width="match_parent"
           android:layout_height="400dip"
           android:id="@+id/webview" ></WebView>
      
      <EditText
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_below="@+id/webview"
           android:id="@+id/edit_text"
           android:layout_centerInParent="true"
           android:hint="Type your message" ></EditText>
      
      <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/button"
           android:layout_centerInParent="true"
           android:layout_toRightOf="@+id/edit_text"
           android:layout_below="@+id/webview"
           android:text="Send"></Button>
      
      
      </RelativeLayout>
      
            

      enter image description here

+4


source


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/webview" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical" >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text"
            android:hint="Type your message" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:text="Send"/>
    </LinearLayout>
</LinearLayout>

      

+1


source


Use relative layout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit_text"
    android:hint="Type your message"
    android:layout_alignParentBottom="true">//align in parent bottom
</EditText>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Send"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@id/edit_text"> //align to the roght of the EditText
</Button>

<WebView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/webview" ></WebView>
</RelativeLayout >

      

+1


source


You can try nesting a linear layout in a relative layout like below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Type your message" >
        </EditText>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send" >
        </Button>
    </LinearLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/container"  >
    </WebView>

</RelativeLayout>

      

If you want an EditText button and a Button on the same line, you will need to use the orientation: horizontal for the LinearLayout.

+1


source


Use this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".9" >
</WebView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="Type your message" >
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Send" >
    </Button>
</LinearLayout>

</LinearLayout>

      

+1


source


You can do this with nested LinearLayouts, or change your LinearLayout root to RelativeLayout. With RelativeLayout, you must add attributes to your WebView, EditText and Button that will indicate how each item should be aligned in relation to other items. See the Relative Layout Guide .

0


source







All Articles