Multiline TextView in Android?
Asked Answered
C

21

177

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?

Cavalry answered 13/7, 2011 at 5:49 Comment(0)
J
215

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>
Juliajulian answered 13/7, 2011 at 6:4 Comment(3)
The OP didn't "want the TextView to always have four lines regardless of the length of the text in it".Fenelia
This answer worked for me. But I got the best results, not with maxlines and lines, but with maxlines and minlines and did not use lines. I set minlines to 1 and maxlines bigger than the biggest data. That way I get 1 to N lines for a compact view of the data.Cattan
@REarleHarris : I too tried the same solution but the problem is it is not taking maxLines count. All data are displayed in minLines count. What can I try now I am using custom textview in the xmlChartulary
F
137

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.

Fairweather answered 27/1, 2015 at 13:5 Comment(7)
This isn't precisely correct as Android's own lint errors suggest that you only use this attribute with EditText and not TextView.Spiracle
Are you referring to the textMultiline? I said not to use that. singleLine is fine on TextViews: developer.android.com/reference/android/widget/…Fairweather
android:singleLine is deprecated nowMaximilien
Are you sure? It doesn't mention that in the docs I link to in the above comment, although I just saw it is marked as such in the res docs. Strange they don't mention that on the TextView page . The trouble is that the workaround they suggest (multiline) stops the view being clickable as I put in my OP. developer.android.com/reference/android/R.attr.html#singleLineFairweather
android:singleLine="false" is deprecated as it's the default valueAnt
@AshrafAlshahawy that's only correct if you don't specify an input type according to the docs. It was a while ago that I made this post, but at that point I certainly had to manually set it to get the desired behaviour (and judging by the fact this is still being upvoted years later, other people have too). Weird huh. Maybe I did set an input type but it's a TextView, not an EditText and I was using it in a menu, so I doubt it.Fairweather
Android Guys are fake. Why cant they put that feature ?Weathers
P
30

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)

Preference answered 5/12, 2013 at 10:25 Comment(2)
android:inputType="textMultiLine" is not for TextView, its for EditText and you get a warning if you try doing thisLuane
You should listen to the warnings. Use @Ryans solution.Medford
Y
21

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"/> 
Yeti answered 8/8, 2015 at 6:3 Comment(3)
this solution cannot be used for setting text dynamicallyChartulary
@SagarDevanga What does it mean to set text dynamically?Newsboy
@SagarDevanga, I have just checked, a dynamic and static texts are multilined by default (if use \n), so it's a right solution. No singleLine=false, no lines=4 and so on.Cherisecherish
R
11

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>
Reddy answered 1/7, 2014 at 10:53 Comment(0)
H
8

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.

Heresiarch answered 17/2, 2017 at 9:42 Comment(0)
M
6

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 :)

Munmro answered 26/3, 2017 at 15:48 Comment(0)
T
5

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.

Turgeon answered 15/11, 2012 at 17:43 Comment(0)
M
3

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.

Maraca answered 19/9, 2017 at 6:52 Comment(0)
H
3

if you want to have at least 4 lines add android:minLines="4"

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

if you want to have always 4 lines add android:lines="4"

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

and if you want to have a maximum of 4 lines add android:maxLines="4"

<TextView android:id="@+id/address1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="left"
    android:maxLines="4"
    android:text="Johar Mor, Gulistan-e-Johar, Karachi" />
Handed answered 21/1, 2019 at 11:14 Comment(2)
This is shown as a warning. Not meant to be used for TextViewBedeck
inputType is only support for EditText components.Erivan
I
2

The best answer I found for multiline TextView is:

android:inputType="textMultiLine"
Isogamy answered 15/6, 2017 at 14:5 Comment(2)
Isn't android:inputType an attribute for <EditText> not for <TextView>?Pappus
A warning appear if you add this attribute to a TextView. - Attributed android:inputType="textMultiLine" should not be used with <TextView>. Actually this attribute is for EditText.Vorster
I
2

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

Isom answered 28/6, 2020 at 11:23 Comment(0)
O
1

I used:

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

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

Onstad answered 10/9, 2014 at 11:9 Comment(0)
D
1

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"/>
Dunedin answered 20/7, 2020 at 18:45 Comment(0)
C
1

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"
/>
Catercousin answered 20/10, 2020 at 9:6 Comment(0)
O
0
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>
Odysseus answered 27/4, 2018 at 10:12 Comment(0)
A
0

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);
    }
Arciform answered 26/1, 2021 at 18:40 Comment(1)
You need to set the input type on a TextView, even if it's not an EditText?Thyrse
E
-1

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 \\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.

Enfilade answered 24/9, 2016 at 3:23 Comment(0)
A
-1

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

<TextView android:id="@+id/kyc_verify_textbox"

    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" />
Agle answered 1/3, 2022 at 9:34 Comment(0)
F
-4

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.

Festivity answered 23/1, 2018 at 11:25 Comment(1)
Please don't do this.Keese
F
-24

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.

Fricandeau answered 13/7, 2011 at 5:59 Comment(3)
What if I want to use fill_parent instead of fixed width? My content is cut, if I do so...Sign
I'll try android:singleLine="false" or android:inputType="textMultiLine"(as described by Marageus).Notice that android:inputType="textMultiLine" takes precedence when both are presentSetula
Sharing the views of others who commented, this is completely wrong and I'm dumbfounded as to why this is the accepted answer.Irina

© 2022 - 2024 — McMap. All rights reserved.