How to wrap text in textview in Android - android

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.

Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs

For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.

For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.

Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy

You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary

This should fix your problem: android:layout_weight="1".

By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.

OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.

In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.

Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.

Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.

I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.

All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"

I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"

Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.

Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>

The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width

You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>

Related

Max lines with height set as wrap_content

I have two TextViews in a vertical LinearLayout, one serves as a display for a book's title and the latter as a display for the book's author(s).
I need the first to have wrap_content as its height, so it takes a good part of the linear layout. However, I want it to cap out at three lines max, so that there is still some space left for the second text view;
and I need the latter to fill the remaining space (0dp and layout_weight=0dp).
I want to use specific configuration so that the author view will be always right after the title view (on its bottom).
Something like this, however the max_lines do not kick in.
I tried to set max_lines to 3 and wrap_content for the first view height, but it seem that max_lines is ignored if height is set to wrap_content.
I also tried to circumvent the problem by sort of cheating and adding a max_height, but then the two views may be spaced apart from one another.
At last I tried to convert the linear layout to a constraint layout, to see if I could access some other layout settings to no avail.
Any help?
It seems to be working fine on my side with android:max_lines="3" even with wrap_content
This is just a test layout I created
<TextView
android:id="#+id/textview1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="#dimen/_13sdp"
android:textStyle="bold"
android:text="Very Very Very Very Very Very Very Very Very Very Very Very Long Text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Very Very Short Text "
app:layout_constraintStart_toStartOf="#+id/textview1"
app:layout_constraintTop_toBottomOf="#+id/textview1" />
Screenshot ->
Hope this helps! :)
Replacing app:max_lines with android:max_lines seems to have fixed the issue.

Android: Cut off to long multiline text in TextView

I have a TextView with a height depending on previous content, but there might be a long Text in it. How can I cut it off at the right point and concatenate three dots or something similar to the new end?
Currently it looks like this:
I found some solutions for Single Line Text, but how does it work with more than one line? I also do not know the number of lines, because this depends on the screen size.
Are there other typical ways on Android to show that the text can be extended? E.g. a colour gradient in the last line?
Edit:
When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:
<TextView
android:id="#+id/podcastShortDesc"
android:text="Long text"
android:layout_below="#+id/podcastTitle"
android:layout_toRightOf="#+id/podcastLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:layout_above="#+id/podcastMoreAction" />
When I do specify maxLines I can have luck an it will work:
But if the title is too big, it does not work:
You should add following code for "3 dots" at the end.
android:ellipsize="end"
You should remove fixed height property
android:layout_height="50dip"
instead you should add number of lines
android:maxLines="4"
android:layout_height="wrap_content"
Android will take care everything else. In this way, even if text is smaller than 4 lines, android will take care size. If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.
Try this. Hope it will work.
<TextView
android:id="#+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>
I have tested 50dp can show two line in normal font size. So in 50dp height you should add maxLines 2.
Try this fix layout_height and add scroll in your textview
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>

Telegram android message cell

I am trying to make a layout like Telegram message box in android.
When the text is short the message and time are aligned in one single line as follows:
But when the message text is longer the time text view is pushed to the bottom of message like this:
How can i achieve this?
P.s. I tried to read the telegram source but wasn't able to figure it out. And i tried to get the size of my text view in adapter but wasn't successful.
Your shown example looks like a RelativeLayout with two TextViews in it (first TextView top-left and second TextView bottom-right).
It should be possible to accomplish this by using a RelativeLayout and two TextViews. The TextViews should be positioned correctly based on the length of the text (the second TextView displaying time should be always in bottom-right corner).
Actually they are not aligned in one line... :D I think you can achieve this by wrapping your TextView for time in RelativeLayout like that:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/background"
android:padding="10dp" >
<TextView style="#style/TextView_port"
android:id="#+id/text"
android:text="Text" />
<RelativeLayout style="#style/LinearLayout"
android:layout_below="#id/text"
android:layout_margin="10dp"
android:gravity="right" >
<TextView style="#style/TextView_port"
android:text="3:59pm"
android:textColor="#73B661" />
</RelativeLayout>
</LinearLayout>
Here i found a trick to set your text alignment to justify:
https://dzone.com/articles/android-%E2%80%93-how-display
EDIT
I finally came up with the solution - the edited code above is for short texts. Check programatically the length of the text and if it is more than 20 for example set orientation of the Linear to vertical and only top margin of the time TextView to -20 - I'll update my answer later if you don't know how :)

Android Layouts - Combining fills with wraps and gravities

I have an oddly organized activity with a number of LinLayouts inside of LinLayouts inside of LinLayouts (inside of LinLayouts) all so that each little section is organized correctly. Everything looks good except for one part.
I have a vertical LinearLayout with two things inside of it. One is another LinearLayout with weight set to 6, and below it is a TextView weight set to 1. What I want to do is have the height of the TextView to scale depending on the amount of lines in it. It could either be a one-liner sentence or maybe a four-line paragraph--I don't know what it'll end up. Then the LinLayout above it needs to fill in the rest of the height.
What layout_height settings do I need to combine to get this to work?
Finally figured it out. It's sort of weird, but this is what I had to do:
<LinearLayout
android:id="#+id/everything_but_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/paragraph" />
I had to have both set to "wrap_content" but give the one I wanted to fill the remaining space a weight WITHOUT giving the other one a weight. Very weird workaround but it works perfectly.

What does ellipsize mean in android?

I've added an EditText to my layout, and added a hint, and made it centered horizontally.
When running the application, the hint was invisible. I found that I should make ellipsize value of the TextView to be start:
<EditText
android:id="#+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:ems="10"
android:gravity="center_horizontal"
android:hint="#string/hint1" />
In Android documentation, I read:
If set, causes words that are longer than the view is wide to be
ellipsized instead of broken in the middle.
The problem is that ellipsize is not found in the dictionary. Can anybody explain to me what benefits we can gain by ellipsize attribute? And what is the difference between start, end, middle?
You can find documentation here.
Based on your requirement you can try according option.
to ellipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.
Say original value pf text view is aaabbbccc and its fitting inside the view
start's output will be : ...bccc
end's output will be : aaab...
middle's output will be : aa...cc
marquee's output will be : aaabbbccc auto sliding from right to left
In my experience, Ellipsis works only if the below two attributes are set.
android:ellipsize="end"
android:singleLine="true"
for the width of TextView, wrap_content or match_parent should both be good.
android:ellipsize added in API Level 1. An ellipsis is three periods in a row. (...) .
In your .xml
<TextView
....
android:text="Hi I am Amiyo, you can see how to ellipse works."
android:ellipsize = "end" />
At this point, the ellipsis will not display yet as a TextView is set to automatically expand on default when new text is entered. You will need to limit TextView in some way. Do this, you can use either add to your TextView a scrollHorizontally, minLines, or maxLines to have the ellipsis display.
To make the ellipse:
at the end: this is how it would.
use: android:ellipsize = "end"
And
in the middle:
use: android:ellipsize = "middle"
And
at the start:
use: android:ellipsize = "start"
And
to have no ellipse
use: android:ellipsize = "none"
Note Please :
Do not use android:singeLine = "true", it is deprecated.
android:maxLines = "1" will not display the three dots (...)
android:lines = "1" will not display the three dots (...)
For more details you can visit here
An ellipsis is three periods in a row...
The TextView will use an ellipsis when it cannot expand to show all of its text. The attribute ellipsized sets the position of the three dots if it is necessary.
Text:
This is my first android application and
I am trying to make a funny game,
It seems android is really very easy to play.
Suppose above is your text and if you are using ellipsize's start attribute it will seen like this
This is my first android application and
...t seems android is really very easy to play.
with end attribute
This is my first android application and
I am trying to make a funny game,...
Here is an example on how ellipsize works without using deprecated android:singleLine="true" in a ConstraintLayout:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="13sp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="long long long long long long text text text" />
remember if you have a text that is supposed to be in a single line, then change the maxLines to 1.
Note: your text must be larger than the container box for the following to marquee:
android:ellipsize="marquee"
Set this property to EditText. Ellipsize is working with disable EditText
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:editable="false"
or setKeyListener(null);
Use ellipsize when you have fixed width, then it will automatically truncate the text & show the ellipsis at end,
it Won't work if you set layout_width as wrap_content & match_parent.
android:width="40dp"
android:ellipsize="end"
android:singleLine="true"
As it is already mentioned above if there is no enough space for text (due to the size of the layout or set limitation) ellpsize will add ... dots to it.
Here an example:
Top: with ellipsize (here it is set to 'end'
Bottom: without

Categories

Resources