8

I want to make my textview multiline.

Image : http://s13.postimg.org/y0q78e1yv/Capture.png

But how can I make my text multiline ?

Which atribut ?

TextView txt_tweet = (TextView) View.inflate(this, R.layout.special_textview, null);

special_textview

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"

    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="5dp"
    android:inputType="textMultiLine"
    android:scrollHorizontally="false">

</TextView>
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
mistermm
  • 81
  • 1
  • 2
  • 8
  • Please do a little research before posting such questions... – Murtaza Khursheed Hussain Jun 09 '15 at 13:50
  • @MurtazaKhursheedHussain i have the problem since three days : http://stackoverflow.com/questions/15032870/create-a-multiline-edittext-programatically i checked something like this, i think 10-20 sites but it doesn't work – mistermm Jun 09 '15 at 13:58

8 Answers8

7

I did it following way:

tv.setElegantTextHeight(true);
tv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tv.setSingleLine(false);
reformed
  • 4,505
  • 11
  • 62
  • 88
5

You want to show to different texts in the same textview? if so, use two text views like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</LinearLayout>

Remote android:inputType="textMultiLine", this is an EditText Atribute.

If you just want to use more then one line in the same text view:

android:maxLines="5"//optional to set max numbers of lines
android:minLines="2"//optional to set min numbers of lines
android:singleLine="false"//set false to allow multiple line
android:lines="2" //or more

If this textview you want to use belongs to a ListView, just use:

android.R.layout.simple_list_item_2

It will give you two texts views to work on.

Kamila Brito
  • 198
  • 8
  • Thank you for your anser, i changed now all to linear layout : http://s27.postimg.org/9mtnp5f3z/Capture.jpg?noCache=1433860180 on the picture you see 3 Rows, the first is hardcoded, and the second and third is with my code from special_textview (is the same textview like the hardcoded) – mistermm Jun 09 '15 at 14:30
2

First replace "\n" with its Html equavalent "&lt;br&gt;" then call Html.fromHtml() on the string. Follow below steps:

String text= model.getMessageBody().toString().replace("\n", "&lt;br&gt;")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

This works perfectly.

Gulzar Bhat
  • 1,255
  • 11
  • 13
2

Adding to the answers: the order matters!

Make sure you call setInputType before setMinLines!

Simon Ninon
  • 2,371
  • 26
  • 43
1

You can do it like this:

txt_tweet.setSingleLine(false);
txt_tweet.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
Ezzored
  • 905
  • 4
  • 10
0

The TextView must have the 'singleLine' attribute set to false. Also you should set the 'ellipsize' to wrap the text:

android:singleLine="false"
android:ellipsize="end"
Chaoz
  • 2,119
  • 1
  • 20
  • 39
0

in layout

android:lines="8"  //Total Lines prior display
android:minLines="6" //Minimum lines
android:maxLines="10" //Maximum Lines
Karol Żygłowicz
  • 2,442
  • 2
  • 26
  • 35
-1

Just remove this line:

 android:inputType="textMultiLine"

inputType for EditTexts

Android Android
  • 724
  • 1
  • 7
  • 20