Eclipse Eclipse - 4 months ago 12
Android Question

How to automatically adjust (enlarge) ListView when making an entry in EditText?

So I have a ListView (for chat messages) and an Edit Text (writing messages). I want that the EditText automatically enlarges when making a blank line. How could I stop that the EditText enlarges to the right when writing >100 characters?

I tried this, but it doesn't work:

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

<TextView
android:id="@+id/xmpp_chatdetail_tv_partnerID"
android:background="@drawable/green_hsrt_1_default_CMYK_100_10_55_0"
android:layout_width="match_parent"
android:layout_height="90sp"
android:gravity="center"
android:padding="6dip"
android:textColor="@color/white"
android:layout_weight="0.125"
android:textSize="20sp">
</TextView>


<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp"
android:paddingBottom="10dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_above="@+id/form"
android:layout_below="@+id/xmpp_chatdetail_tv_partnerID" />

<LinearLayout
android:id="@+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:paddingBottom="2dp" >


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">

<EditText
android:id="@+id/editText_xmpp_chat"
android:layout_width="1sp"
android:layout_height="wrap_content"
android:layout_weight="0.61"
android:ems="10"
android:maxHeight="80sp"
android:hint="@string/hint_text"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textSize="25sp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btn_xmpp_send"
android:layout_toStartOf="@+id/btn_xmpp_send"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="255"
android:maxLines="4" />

<ImageButton
android:id="@+id/btn_xmpp_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_send_xmpp"
android:text="@string/send"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>

</LinearLayout>


</RelativeLayout>


Thanks in advance!

Answer

This layout does the trick. Try it out and let me know if it works.

I removed the layout_weight attributes because they don't work for Relative Layouts.

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

    <TextView
        android:id="@+id/xmpp_chatdetail_tv_partnerID"
        android:background="@drawable/green_hsrt_1_default_CMYK_100_10_55_0"
        android:layout_width="match_parent"
        android:layout_height="90sp"
        android:gravity="center"
        android:padding="6dip"
        android:textColor="@color/white"
        android:layout_weight="0.125"
        android:textSize="20sp">
    </TextView>

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingBottom="10dp"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_above="@+id/form"
        android:layout_below="@+id/xmpp_chatdetail_tv_partnerID" />

    <RelativeLayout
        android:id="@+id/form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:paddingBottom="2dp">

        <EditText
            android:id="@+id/editText_xmpp_chat"
            android:text="adasd asdasdas dasdsad asdsad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxHeight="80sp"
            android:hint="@string/hint_text"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"
            android:textSize="25sp"
            android:layout_toLeftOf="@+id/btn_xmpp_send"
            android:layout_toStartOf="@+id/btn_xmpp_send"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLength="255"
            android:maxLines="4" />

        <ImageButton
            android:id="@+id/btn_xmpp_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_send_xmpp"
            android:text="@string/send"
            android:layout_alignBottom="@+id/editText_xmpp_chat"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
</RelativeLayout>
Comments