Reputation: 377
I'm trying to get user comment and after pressing the enter button, the text just gets hide due to a line change. I m trying to expand my EditText View so that I can show at least 4 lines of comment.
<LinearLayout
android:id="@+id/comments_body_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#fff"
android:elevation="8dp"
android:gravity="right"
android:maxHeight="150dp"
android:orientation="horizontal"
android:weightSum="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/comments_text_body"
android:layout_width="151dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_weight="8"
android:inputType="textCapSentences|textMultiLine"
android:maxHeight="100dp"
android:maxLength="2000"
android:maxLines="4"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp" />
<ImageView
android:id="@+id/comment_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="12dp"
app:srcCompat="@drawable/ic_send_black_24dp" />
</LinearLayout>
Above is my current code, can someone tell me what's wrong with my XML file?
I added the following two attributes too
android:maxLength="2000"
android:maxLines="4"
Upvotes: 1
Views: 1436
Reputation: 317
Before starting, you need to make a few changes to your code.
android:layout_weight
attribute, you should always set your width attribute as android:layout_width="0dp"
android:layout_alignParentBottom
and android:layout_alignParentEnd
. They are invalid for this case.ImageView
under the EditText
, it would be better if you assigned a height to your view instead of match_parent
, or just use wrap_content
if you don't need a fixed size.You mentioned you needed to display at least 4 lines of text in your EditText
. If that is required for all use cases, add the attribute android:minLines="4"
to your EditText
. If you want a maximum of 4 lines to be displayed, then your code android:maxLines="4"
works perfectly.
To prevent the issue where the text gets hidden as the number of lines increases, add the attribute android:scrollbars="vertical"
to the EditText
.
Your final EditText XML may look something like (assuming 4 lines are needed at a minimum. If not, just remove the android:minLines
attribute)
<EditText
android:id="@+id/comments_text_body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:inputType="textCapSentences|textMultiLine"
android:maxHeight="100dp"
android:minLength="2000"
android:minLines="4"
android:scrollbars="vertical"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp" />
Upvotes: 0
Reputation: 1306
It looks like you have the EditText
set to match_parent
.
Try changing
android:layout_height="match_parent"
to
android:layout_height="wrap_content"
Upvotes: 0
Reputation: 1184
First I cleaned up your code
<LinearLayout
android:id="@+id/comments_body_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#fff"
android:elevation="8dp"
android:gravity="right"
android:maxHeight="150dp"
android:orientation="horizontal"
android:weightSum="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/comments_text_body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:inputType="textCapSentences|textMultiLine"
android:maxHeight="100dp"
android:maxLength="2000"
android:maxLines="4"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp" />
<ImageView
android:id="@+id/comment_send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="12dp"
app:srcCompat="@drawable/ic_send_black_24dp" />
1) You should use layout_width="0dp" as you use layout_weight in the LinearLayout
2) do NOT use android:layout_alignParentBottom="true" and android:layout_alignParentEnd="true" as it doesn't make sense in the LinearLayout
3) And use layout_height="wrap_content" is the EditText
Upvotes: 2
Reputation: 12803
You need to add this into your EditText attribute
android:lines="4"
Also, set height to wrap_content
android:layout_height="wrap_content"
Upvotes: 3