Multiline TextView in Android?

AndroidTextviewTablelayoutMultiline

Android Problem Overview


I did like below in xml

<TableRow>
    <TextView android:id="@+id/address1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4" 
        android:singleLine="false" 				
        android:text="Johar Mor, Gulistan-e-Johar, Karachi" >
    </TextView> 
</TableRow>

It is not working for multiline, and I am using TableLayout...

so what is mistake I am doing here?

Android Solutions


Solution 1 - Android

If the text you're putting in the TextView is short, it will not automatically expand to four lines. If you want the TextView to always have four lines regardless of the length of the text in it, set the android:lines attribute:

<TextView
	android:id="@+id/address1"
	android:gravity="left"
	android:layout_height="fill_parent"
	android:layout_width="wrap_content"
	android:maxLines="4"
	android:lines="4"
	android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

You can do this with TableRow, see below code

<TableRow >
        <TextView
            android:id="@+id/tv_description_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="8dp"
            android:text="@string/rating_review"
            android:textColor="@color/black"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:maxLines="4"`enter code here`
            android:padding="8dp"
            android:text="The food test was very good."
            android:textColor="@color/black"
            android:textColorHint="@color/hint_text_color" />
    </TableRow>

Solution 2 - Android

I just thought I'd add that if you use :

android:inputType="textMultiLine"

Then the view stops being clickable. I was trying to get multi line textviews in my slide-drawer menu which obviously needs to respond to clicks.

The android:singleLine="false" worked fine though.

Solution 3 - Android

I like neither of the answers. Simply set the inputType and the TextView will adapt to its content

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>

Tested on a Nexus One (2.3) and Nexus 4 (4.4)

Solution 4 - Android

Simply put '\n' inside your text... no extra attribute required :)

          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Pigeon\nControl\nServices"
            android:id="@+id/textView5"
            android:layout_gravity="center"
            android:paddingLeft="12dp"/> 

Solution 5 - Android

Just add textview in ScrollView

<ScrollView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:layout_marginLeft="15dp"
           android:layout_marginRight="15dp"
           android:layout_marginTop="20dp"
           android:fillViewport="true">
           
           <TextView
               android:id="@+id/txtquestion"
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="@drawable/abs__dialog_full_holo_light"
               android:lines="20"
               android:scrollHorizontally="false"
               android:scrollbars="vertical"
               android:textSize="15sp" />
           
       </ScrollView>

Solution 6 - Android

What I learned was to add "\n" in between the words where you want it to brake into the next line. For example...

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="M-F 9am-5pm \n By Appointment Only" />

The \n between 5pm and By allowed me more control of where I wanted my my next line to begin and end.

Solution 7 - Android

I do not like the solution that forces the number of lines in the text view. I rather suggest you solve it via the solution proposed here. As I see the OP is also struggling with making text view look like proper in table and shrinkColumns is the correct directive to pass in to achieve what is wanted.

Solution 8 - Android

Try to work with EditText by make it unclickable and unfocusable, also you can display a scrollbar and delete the EditText's underbar.
Here is an example:

<EditText
android:id="@+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:inputType="textMultiLine"                   <!-- multiline -->
android:clickable="false"                         <!-- unclickable -->
android:focusable="false"                         <!-- unfocusable -->
android:scrollbars="vertical"     <!-- enable scrolling vertically -->
android:background="@android:color/transparent"   <!-- hide the underbar of EditText -->
/>  

Hope this helps :)

Solution 9 - Android

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.

Solution 10 - Android

The best answer I found for multiline TextView is:

android:inputType="textMultiLine"

Solution 11 - Android

you can use android:inputType="textMultiLine"

  <TextView android:id="@+id/address1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="left"
    android:maxLines="4" 
    android:inputType="textMultiLine"
    android:text="Johar Mor, Gulistan-e-Johar, Karachi" />

Solution 12 - Android

Simplest Way

<TableRow>
    <TextView android:id="@+id/address1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4" 
        android:singleLine="false"              
        android:text="Johar Mor,\n Gulistan-e-Johar,\n Karachi" >
    </TextView> 
</TableRow>

Use \n where you want to insert a new line Hopefully it will help you

Solution 13 - Android

I hope these answers are little bit old, just change the input type in resource layout file will solve your problem. For example:

<EditText
        android:id="@+id/notesInput"
        android:hint="Notes"
        android:inputType="textMultiLine"
/>

Solution 14 - Android

I used:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

it worked for me. 1 is the number of column.

Solution 15 - Android

You can do this with TableRow, see below code

<TableRow >
    <TextView
        android:id="@+id/tv_description_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="8dp"
        android:text="@string/rating_review"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4"`enter code here`
        android:padding="8dp"
        android:text="The food test was very good."
        android:textColor="@color/black"
        android:textColorHint="@color/hint_text_color" />
</TableRow>

Solution 16 - Android

The key is a Textview with singleline=false (which yes is deprecated, but is a must have to work) combined with lines, maxlinesor minlines

<TextView
                android:id="@+id/txtBowlers"
                style="@style/Default_TextBox.Small"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:singleLine="false"
                android:maxLines="6"
                android:text="Bob\nSally\nJohn"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/txtTeamName"
                app:layout_constraintTop_toBottomOf="@+id/txtTeamName"/>

Solution 17 - Android

Below code can work for Single line and Multi-line textview

isMultiLine = If true then Textview showing with Multi-line otherwise single line

    if (isMultiLine) {
        textView.setElegantTextHeight(true);
        textView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        textView.setSingleLine(false);
        
    } else {
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
    }

Solution 18 - Android

no need to add anything just use "\n" at the points where you want to change the lines e.g

    android:layout_width="370dp"
    android:layout_height="99dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="13dp"

    android:text="For KYC verification, \n please submit: \n 1 RC photo \n 2 Truck Owner Details \n Note:Both are mandatory for KYC verification"
    app:layout_constraintBottom_toTopOf="@+id/divider2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/divider" />

Solution 19 - Android

Why don't you declare a string instead of writing a long lines into the layout file. For this you have to declare a line of code into layout file 'android:text="@string/text"' and go to the <ProjectName>\app\src\main\res\values\strings.xml and add a line of code ' Your multiline text here' Also use '\n' to break the line from any point else the line will automatically be adjusted.

Solution 20 - Android

for me non of the solutions did not work. I know it is not a complete answer for this question, but sometimes it can help.

I put 4 textviews in a vertical linearlayout.

<Linearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation:"vertical"

    ...>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line1"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line2"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line3"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line4"
       .../>
</LinearLayout>

This solution is good for cases that the text view text is constant, like labels.

Solution 21 - Android

TextView will be multi line when it wont get enough space to fit in the single line and singLine not set to true.

If it gets the space in one line it wont be multi line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChatar Veer SutharView Question on Stackoverflow
Solution 1 - AndroidZarahView Answer on Stackoverflow
Solution 2 - AndroidRyanView Answer on Stackoverflow
Solution 3 - AndroidMaraguesView Answer on Stackoverflow
Solution 4 - AndroidNaunidh SinghView Answer on Stackoverflow
Solution 5 - AndroidPravin BhosaleView Answer on Stackoverflow
Solution 6 - AndroidEduardo LopezView Answer on Stackoverflow
Solution 7 - AndroidBoris StrandjevView Answer on Stackoverflow
Solution 8 - AndroidKhalil MegView Answer on Stackoverflow
Solution 9 - AndroidGulzar BhatView Answer on Stackoverflow
Solution 10 - AndroidBhuvan BhattView Answer on Stackoverflow
Solution 11 - AndroidEnakhiView Answer on Stackoverflow
Solution 12 - AndroidDavinder KambojView Answer on Stackoverflow
Solution 13 - AndroidAathil AhamedView Answer on Stackoverflow
Solution 14 - AndroidSusana Mar FloresView Answer on Stackoverflow
Solution 15 - AndroidJitendra NandiyaView Answer on Stackoverflow
Solution 16 - AndroidSnegView Answer on Stackoverflow
Solution 17 - AndroidPratik DodiyaView Answer on Stackoverflow
Solution 18 - AndroidSatish SinghView Answer on Stackoverflow
Solution 19 - AndroidTara Prasad SharmaView Answer on Stackoverflow
Solution 20 - Androidsaleh sereshkiView Answer on Stackoverflow
Solution 21 - AndroidRaselView Answer on Stackoverflow