Search code examples
androidxmlandroid-layoutandroid-linearlayoutandroid-relativelayout

Organizing layouts with edit text at bottom?


I wanted to make a layout with 3 LinearLayouts. In the 2nd LinearLayout I would have a ListView and the 3rd LinearLayout I would have an EditText and Button at the bottom. This was my attempt:

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

    <LinearLayout
        android:id="@+id/view_post"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="@color/com_facebook_blue">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/view_status"/>

    </LinearLayout>

        <LinearLayout
            android:id="@+id/view_comments"
            android:layout_below="@+id/view_post"
            android:layout_width="match_parent"
            android:layout_height="250dp">

            <ListView
                android:id="@+id/lv_comments_feed"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </ListView>

        </LinearLayout>

        <LinearLayout
            android:layout_below="@+id/view_comments"
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_weight="5"
                android:gravity="top|left"
                android:inputType="textMultiLine"
                android:scrollHorizontally="false" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:text="send"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

</RelativeLayout>

Now the design looks great but when I load the app on my phone, I have 2 problems:

  1. I don't want to hardcode the LinearLayout heights. I have looked into layout_weight but I can't seem to have these layout_weights work without running into the 2nd problem below:

  2. When I click the EditText, the EditText is hidden even though I have declared android:windowSoftInputMode="adjustResize|stateHidden" in my activity and I'm not sure why that's happening. Also, the EditText and Button are not directly at the bottom of the screen which is what I would want. Any help is appreciated, thanks!


Solution

  • you don't need to add listview as child of linear layout. and avoid fill_parent & use match_parent. if first linear layout is only for textview then don't use it inside of linearlayout. or only use wrap_content instead of a specific layout_height

    Here i made some changes hope it will work

    <?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:orientation="vertical">
    
    <LinearLayout
        android:id="@+id/view_post"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#222dc9">
    
        <TextView
            android:id="@+id/view_status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/send_message"
        android:layout_below="@id/view_post"
       ></ListView>
    
    
    <LinearLayout
        android:id="@+id/send_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_alignParentBottom="true"
    
        android:orientation="horizontal">
    
        <EditText
            android:id="@+id/write_comment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="5"
            android:gravity="top|left"
            android:inputType="textMultiLine"
            android:scrollHorizontally="false" />
    
        <Button
            android:id="@+id/send_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center"
            android:text="send"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
    
    </RelativeLayout>
    

    and use in Manifest with activity name is

    android:windowSoftInputMode="adjustPan"