Arjun Singh Arjun Singh - 4 months ago 8
Android Question

Show Edit Text in Text View, & adjust height according to Content written in Edit text (Android)

When i send message or joke of 1 Line or 1 word, then why so much space is left at bottom. I take text view as

wrap_content
.

enter image description here

I need text to be wrapped to its height. What I needed is shown in the picture below.
enter image description here

Text View



<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:ems="10"
android:gravity="top|left"
android:hint="Enter Message"
android:inputType="textMultiLine|textPostalAddress"
android:minHeight="200dip"
android:padding="5dip"
android:scrollbars="vertical"
android:layout_marginTop="-5dp"
android:layout_marginBottom="-5dp"
android:textColor="@color/blue"
android:maxLines="1000000"
android:id="@+id/messageDetail"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/linearLayout4" />


Edit Text, both are in different Activities



<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="top|left"
android:hint="Enter Message"
android:id="@+id/sendTextView"
android:inputType="textMultiLine|textPostalAddress"
android:minHeight="200dip"
android:minWidth="390dp"
android:editable="true"
android:focusable="true"
android:clickable="true"
android:cursorVisible="true"
android:scrollbars="vertical"
android:background="@color/gray"
android:padding="@dimen/fab_margin"
android:layout_above="@+id/camera"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

Answer

Try this to adjust height according to Content written in Edit text,

               <EditText
                    android:id="@+id/editMsj"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:gravity="top|left"
                    android:hint="Enter Message"
                    android:inputType="textMultiLine|textPostalAddress"
                    android:minHeight="200dip"
                    android:padding="5dip"
                    android:scrollbars="vertical"
                    android:textColor="@color/black"
                    android:textColorHint="#80000000"
                    android:textSize="@dimen/font_normal_size" />

For TextView, here i have added android:ellipsize="end" , android:maxLines="20" , android:singleLine="false"

<TextView
        android:id="@+id/messageDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:hint="Your Message"
        android:maxLines="20" //number of lines to restrict in textview
        android:padding="5dip"
        android:singleLine="false" />

Hope this helps

Comments