TextView text multiple lines for Android v4.1+ - android

I have a TextView in Android with a Text that can vary quite a bit in length. It could be a single word, it could also be more than ten full sentences.
My TextView has android:layout_width="match_parent" and android:layout_height="wrap_content". I've looked around and found a lot of ways to make it wrap over multiple lines (some requiring multiple of these):
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none"
android:maxLines="10"
etc.
So my question is: Which should I use to wrap my TextView-Text in Android version 4.1+?
PS: I haven't tested any of these yet, but since I've found so many different answers on SO-questions about Text-Wrapping, I was wondering what the "best" method is (for my Android version).

I ended up using:
<TextView
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:scrollHorizontally="false"
android:ellipsize="end"
android:maxLines="10" />
When I looked at the singleLine in Eclipse it gave the following java-doc:
Constrains the text to a single horizontally scrolling line instead of
letting it wrap onto multiple lines, and advances focus instead of
inserting a newline when you press the enter key. * Deprecated: This
attribute is deprecated. Use "maxLines" instead to change the layout
of a static text, and use the "textMultiLine" flag in the inputType
attribute instead for editable text views (if both singleLine and
inputType are supplied, the inputType flags will override the value
of singleLine). [boolean]
So I now use maxLines="10", since I don't want an entire life-story to be inside the TextView, 10 lines should be a good maximum. I've added the ellipsize="end" to have three dots (...) at the end of the text when it has more than 10 lines. And the scrollHorizontally="false" does the trick of allowing multiple lines without a horizontal scroll-bar.

try setting this attributes:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:singleLine="false"
here
to elipsize, 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 out put will be : aaabbbccc auto sliding from right to left

Related

Android EditText Multiline does not work as it should

I'm pretty desperate about this feature.
I tried pretty much everything there is to find to made these EditTexts multiline enabled,
but they just keep going on a single line scrolling the entire EditText with it.
How hard can it be to stop at the end of the border of the EditText and move to the next line?
I have this activity with an EditText and 2 buttons. One of these buttons adds a predetermined line of text to the EditText. The other puts the EditText's text into some form of object that I use later in the app.
However I can't get this multiline feature to work.. I've tried limiting the size. Setting the multiline flag. Disabling singleline. Giving lines, and minLines a random number (10).
Disabling horizontalscroll on the EditText. But nothing works....
Can anyone tell me what the hell I'm doing wrong? And how I can fix this horrid abomination of an EditText.
This is how my nightmare looks like now.
<EditText
android:id="#+id/callofedittext"
android:layout_width="wrap_content"
android:inputType="textMultiLine"
android:width="300dp"
android:minLines="10"
android:layout_height="wrap_content"
android:gravity="top|left"
android:textColor="#color/textWhite"
android:background="#color/textBlack"
android:paddingLeft="3dp"
android:singleLine="false"
android:scrollHorizontally="false"
>
<requestFocus />
</EditText>
It haunts my dreams...
EDIT: > Light at the end of the tunnel.
While I was focussing on the xml.. A new clean project pointed out to me that EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); is causing all of my problems. Not specifically the properties inside the xml.
From this comment, the inputType was set in the code as well with:
textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
This is actually not correct, because TYPE_TEXT_FLAG_MULTI_LINE and TYPE_TEXT_FLAG_CAP_SENTENCES are only flags, and do not contain the actual input type. In order for them to work, they must be layered as flags on top of InputType.TYPE_CLASS_TEXT. Without this type class flag, the edit text does not have a base input type class to apply your flags to, and defaults to no specified input type.
So, the correct way to set the input type with both of these flags is:
textMessage.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
For official details on how these flags work, see the Android Developer Docs on InputType and TextView - android:inputType
I'm not sure why the design decision is this. Personally, I think they should have hidden how they are representing their flags (as ints/bit flags), and instead had enums and/or subclasses of InputType for their public interface.
hey you have to add the following code in xml file ..
android:gravity="top"
android:maxLines="4"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:textSize="16sp"
android:padding="10dp"
and you have to put activity file ...
edtComment = (EditText) findViewById(R.id.edtComment);
edtComment.setMovementMethod(new ScrollingMovementMethod());
this is works for me and hope it will works for you .....
Have you tried using
android:layout_height="wrap content"
instead of
android:layout_height="match_parent"

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

Vertical text spacing changed in Android 4.0

I have an app that allows the user to change the text size. On Android 2.X when you made the text smaller the vertical spacing got smaller too. On 4.X the text gets smaller and the spacing remains the same.
The call that I am using to change the size is (there is a loop to change multiple lines):
tvData[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, fSize);
The text fields are described in main.xml as follows (there is more than one and they are each "below" the previous one.
<TextView android:id="#+id/textOut1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:typeface="monospace"
android:textSize="28dip"
android:textColor="#000000"
android:textStyle="bold"
android:layout_below="#id/textOut0"
/>
What do I have to do so this works in both old and new Android?
The fix was to not put each line of text in a separate TextView. By putting all of the text in one TextView and ending each line in a newline it now adjusts vertical spacing when the font size is changed.
And the change to one has been tested to work on older versions of Android too.
I cannot remember why I used an array of TextViews in the first place, seems like a bad choice looking at it now, but it did work with older versions.
TomZ

How to wrap text in textview in 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>

inputType makes item un-clickable

I have a ListView in which each item has a complex layout that contains, at some point, a TextView with android:inputType="text" and android:ellipsize="marquee". My problem is that inputType="text" does something that renders the whole listview item un-clickable. I've tried:
android:descendantFocusability="blocksDescendants" on the top-most layout of an item,
android:focusable="false" on the TextView itself,
android:focusableInTouchMode="false" on the TextView itself,
android:clickable="false" on the TextView itself,
android:editable="false" on the TextView itself.
Nothing worked.
The reason why I use android:inputType="text" on a TextView is so that it becomes single-line and android:ellipsize="marquee" actually works. I've done my homework:
android:singleLine is deprecated*
android:lines="1", as suggested here, doesn't work, the text still wraps, you just don't get to see the second line, so the marquee effect does not appear.
* or is it? My Ctrl+Space in Eclipse says this about android:singleLine (emphasis mine):
Constrains the text to a single horizontally scrolling line instead of
letting it wrap onto multiple lines, and advances focus instead of
inserting a newline when you press the enter key. * Deprecated:
This attribute is deprecated and is replaced by the textMultiLine
flag in the inputType attribute. Use caution when altering
existing layouts, as the default value of singeLine is false (multi-
line mode), but if you specify any value for inputType, the default
is single-line mode. (If both singleLine and inputType attributes
are found, the inputType flags will override the value of
singleLine.). [boolean]
However, the docs do not say anything about any deprecation.
What's going on here?
Actually in the official documentation of R.attr that attribute constant is deprecated.
However (as mentioned) this is contradicting the TextView documentation page. And when looking at the related methods, setting the singleLine attribute is equivalent to :
myTextView.setTransformationMethod(new SingleLineTransformationMethod());
which too isn't deprecated. And it is how i dance around that deprecation.
Not sure if I fully understand your needs, however if I do here is the code I used for list item (it has image on the left and text on the right, the text is limited with 2 lines and uses ellipsize feature):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="5dip">
<ImageView android:id="#+id/list_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10px" />
<TextView android:id="#+id/list_item_label"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:maxLines="2"
android:inputType="textMultiLine" />
</LinearLayout>
If you need 1 line limit probably try to use android:maxLines="1"?
As long as the official, online documentation does not mention that singleLine is deprecated, I will not consider it so. It must be a bug in the SDK. I will use singleLine for as long as it is not marked as deprecated in the documentation and until there's a fully working, non-deprecated alternative.

Categories

Resources