I know this question already asked multiple times but I could not able to solve my problem with available solutions.
I applied all solutions which are available on other stack overflow threads.
Requirements :
If string length is more than 140 than show only 140 characters and at last add ".." at last.
If string is less than 140 characters then no need to append ".." at last.
What I have tried:
<TextView
android:id="#+id/tvIssueBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="140"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="#dimen/detail_text_size"/>
I have tried above code but it is not working and I tried all other properties mixture also but It is not working.
Shall I have to do with str.subString(0,141) option? There is no way to limit 140 characters using textview property?
Thanks to all in advanced. Any help will be appreciated.
There's no way to get this to work exactly as you want with a built in TextView. First off, a TextView will not allow anyone to add more than maxLength characters. So if they try to add 200 characters, the last 60 would be dropped. This means even 141 won't work if you want to keep that data. And Elipsize doesn't ellipsize where you want it- it ellipsizes where it needs to in order to fit the text on screen. You can't force it to do so after a given length.
What you want really need is a custom view or a subclass of TextView, so you can override onDraw to draw a different string than the set text (a hand ellipsized version of it).
Related
I have something like a message system and listview that hold the messages.So I want to show let's say only the first 10 symbols of the message and after ellipsis.I couldn't find information how to do it, so if somebody can help me.
I assume you know how to set up an adapter.
You can achieve that by editing your XML layout file. You limit the max amount of text your TextView can hold, and tell it to add ellipsis if it's longer.
<TextView
android:id="#+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>
Example from here
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
I think an image will explain this far better than words. The screenshot is from the layout viewer thingy in Intellij, but this happens on device as well. I want the cut off text to not be displayed at all. Is this possible?
<TextView
....
android:lines="2"
or:
<TextView
....
android:maxLines="2"
First option sets exact number of lines for your text view, second options sets the maximum number.
I use the following RoboTextView xml declaration:
<com.package.utils.RobotoTextView
android:id="#+id/ttli_small_desc"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="6dp"
android:textSize="12dp"
android:paddingBottom="6dp"
android:maxLines="5"
android:ellipsize="end"
android:textColor="#color/selectable_text_black_to_white"
bit:fontType="light"/>
It is a custom view object, but I've tested it with a standard TextView and the same problem occurs. The only thing this custom view does is allow me to use RobotoLight font. A bit outdated of a technique now but it's there so I use it.
Anyways what I'd expect is a maximum of 5 lines which should look like this
word words words
words words words
word word words
words word
words words words...
But here is what I'm getting:
Basically it ellipsizes the text, but then just picks right back up afterwards. I've played around with singleLine=false and other random attributes trying to fix it but I've struck out so far. Does anyone know how to fix this?
Thanks!
My question may sound a bit weird but I'll try to explain it better here.
I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:
android:maxLines="2"
Okay, now we've got it limited to 2 lines.
Then, in my Activity, I make:
termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);
Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.
Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:
Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.
Thanks and I hope you can understand my problem.
You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.
public void setEllipsize (TextUtils.TruncateAt where) Added in API
level 1
Causes words in the text that are longer than the view is wide to be
ellipsized instead of broken in the middle. You may also want to
setSingleLine() or setHorizontallyScrolling(boolean) to constrain the
text to a single line. Use null to turn off ellipsizing. If
setMaxLines(int) has been used to set two or more lines, END and
MARQUEE* are only supported (other ellipsizing types will not do
anything).
Related XML Attributes
android:ellipsize
Might be a bit hacky, but here's my suggestion:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="your_text_view"
android:text="long long long text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="link"
android:text="View More"
android:onClick="addMoreLinesAndHideThisTextView"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Some additional tweaking may be needed, but I think you got the idea.