How do I display a TextArea for my android project? From xml, the only choice is TextField, multi lined. But thats editable. I need a TextArea which is only for displaying messages/text can't be edit/input by the user.
-
You need something in style like EditText field or TextView? – Robert Dec 29 '11 at 10:45
-
Use Text-view for this purpose it is the best suitable for your case. – Dharmendra Dec 29 '11 at 10:47
-
the problem of textview i find it shows what ever is it of my background which i had images making reading difficult – Kenneth Lhv Dec 29 '11 at 11:03
-
use background color to TextView, it solves your problem. – Yugandhar Babu Dec 29 '11 at 11:08
-
Try to use `EditText` view - [EditText documentation](http://developer.android.com/reference/android/widget/EditText.html). – Natali Dec 29 '11 at 10:37
-
Try this code, will help you sure. https://stackoverflow.com/a/47813929/3448003 – Bajrang Hudda Dec 14 '17 at 13:11
12 Answers
Try this:
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="150dp"
android:inputType="text|textMultiLine"
android:gravity="top"/>

- 681
- 1
- 6
- 3
Use TextView
inside a ScrollView
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="150dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>

- 5,823
- 1
- 38
- 36

- 53,011
- 55
- 178
- 243
If you do not want to allow user to enter text TextView
is the best option here. Any how you can also add EditText
for this purpose. here is a sample code for that.
This would automatically show scrollbar if there is more text than the specified lines.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="8"
android:maxLines="10"
android:minLines="6"
android:scrollbars="vertical" />
Edit: Adding attributes below to textView
would make it a textArea
that would not be editable.
android:lines="8"
android:maxLines="10"
android:minLines="6" // optional

- 6,548
- 8
- 42
- 69
<EditText
android:layout_width="match_parent"
android:layout_height="160dp"
android:ems="10"
android:gravity="left|top"
android:hint="Write your comment.."
android:inputType="textMultiLine"
android:textSize="15sp">
<requestFocus />
</EditText>

- 240
- 3
- 8
-
Thank you bro. It helped me very much. But may i know why you used android:ems="10" and
– Akshay Masram Dec 20 '22 at 13:16
Use TextView
inside a ScrollView
to display messages with any no.of lines. User can't edit the text in this view as in EditText
.
I think this is good for your requirement. Try it once.
You can change the default color and text size in XML file only if you want to fix them as below:
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="100px"
android:textColor="#f00"
android:textSize="25px"
android:typeface="serif"
android:textStyle="italic"/>
or if you want to change dynamically whenever you want use as below:
TextView textarea = (TextView)findViewById(R.id.tv); // tv is id in XML file for TextView
textarea.setTextSize(20);
textarea.setTextColor(Color.rgb(0xff, 0, 0));
textarea.setTypeface(Typeface.SERIF, Typeface.ITALIC);

- 6,511
- 13
- 49
- 53

- 10,311
- 9
- 42
- 67
-
-
Yes it is very good for my requirement. Now how do i changes its font/size/color ? Default text seem to be gray faded-ish, which visibility is poor. – Kenneth Lhv Jan 03 '12 at 09:57
-
in where should i put textarea.setTextSize(20); ,it doesnt look like it is for xml. – Kenneth Lhv Jan 03 '12 at 18:41
-
-
Yes, i am using textview now instead of edittext. Can you check my other questions? – Kenneth Lhv Jan 04 '12 at 05:55
-
-
here http://stackoverflow.com/questions/8717385/android-how-to-link-xml-with-button – Kenneth Lhv Jan 04 '12 at 06:01
<EditText
android:id="@+id/comments_textbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="comments"
android:inputType="textMultiLine"
android:longClickable="false" />
use it to create multi line text box like textArea in Html

- 3,374
- 4
- 33
- 50

- 361
- 3
- 3
Defining an Android Mulitline EditText Field is done via the inputType=”textMultiline”. Unfortunately the text looks strangely aligned. To solve that also use the gravity=”left|top” attribute.
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine" >
<requestFocus />
check this vogella blog

- 714
- 8
- 8
All of the answers are good but not complete. Use this.
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="@drawable/text_area_background"
android:gravity="start|top"
android:hint="@string/write_your_comments"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="textMultiLine"
android:padding="12dp" />

- 3,981
- 2
- 26
- 49
this short answer might help someone who is looking to do. add this simple code in your code
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:singleLine="true" />

- 1,998
- 2
- 25
- 40
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="100dp">
<EditText
android:id="@+id/question_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:inputType="text|textMultiLine"
android:lineSpacingExtra="5sp"
android:padding="5dp"
android:textAlignment="textEnd"
android:typeface="normal" />
</android.support.v4.widget.NestedScrollView>

- 584
- 5
- 9
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/border_layout"
android:inputType="text|textMultiLine"
android:gravity="top"/>
Create a Drawable Resource file called border_layout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/custom_grey" />
</shape>
custom grey color under values->colors.xml
<color name="custom_grey">#494646</color>

- 11
- 1