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 messa
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);
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" />
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
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>
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" />
<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>