TextView setText problem - android

I have a TextView and my need is to add 10 characters in that TextView by code. But when characters are greater than 10, they should be display in TextView but with 8 character + .. of that charsequence. And when I want to read text of that TextView I must get full charsequence not 8 character + .. . For example
tv.settext("ASDFGHJKLQ") // this is 10 characters long, so no rule required
but
tv.settext("ASDFGHJKLOP") // this is more than 10 characters then it should be display as
ASDFGHJK.. in textView, but when I retrieve the value of textview, it should return ASDFGHJKLOP instead of ASDFGHJK.. , so how can it be done.
That textView is row of a listview.

Try adding this to your TextView (marquee or end):
android:ellipsize="marquee"
You may also need:
android:scrollHorizontally="true"
Without scrollHorizontally="true" you may still get the text wrapped rather than appended with ... I use these very 2 lines to display a list view in my app. The 3 long lines I have get appended correctly.

This truncating happens because it does not fit in your list item. Reduce the font ?
or ellipsize ?

I see only one solution. But maybe there are another solutions.
To store you string.
If string's length is greater than 10 letters set text of your TextView with 12345678..
And if you want to get right text you should take the value of the string and not of the textview.

Related

Is it possible to have TextView with maxLines=2 but if the text has one single long word not break it in 2 lines?

I have a TextView and I set text which I receive from Backend.
the text is either from 1 to 3 words.
Maximum the textview can be 2 lines.
I am using setAutoSizeTextTypeUniformWithConfiguration
and text.breakStrategy = LineBreaker.BREAK_STRATEGY_SIMPLE
And I don't have any success.
wondering is it possible if the text is single word I don't want to split it. I would like to have it in single line with small textSize. if the text is 2 words and long I am fine to show it in 2 lines. The problem is it always breaks the word in 2 lines if it is long while I don't want.
The only solution that comes to my mind is before setting the text to a textview, check whether text has spaces in it.
If it has not that that means it's a single word, so set ptogrammatically maxlines to 1, otherwise set maxlines to 2.
EDIT: I missed text size part. I use this library for autoresizing and it worked so far https://github.com/jivimberg/AutoResizeTextView
it's automatically handled in androidx.appcompat.widget.AppCompatEditText and just set android:maxLines="2"
remove all other properties you set

TextView no wrapping special character Android

Is there a way to create/override the custom word wrap engine for Android TextView?
For example,
My name is Edwin (US:1113)
Should be displayed as
My name is Edwin
(US:1113)
Instead of
My name is Edwin (US:
1113)
Already tried changing all the possible value for the setting below but nothing good.
Android:maxLines
Android:setHorizontallyScrolling
Android:ellipsize
Android:singleLine
In my findings, word wrap in TextView does not include special characters such as :.
Updated 3.09AM 2/7/2018 UTC
As mentioned by #Umair, line breaking with "\n" is not possible because I don't know the length of the text before the colon :. This will line break the sentence too early before it reached the maximum width of the TextView.
Use 2 TextViews inside a LinearLayout with horizontal orientation and put My name is Edwin in the 1st and (US:1113) in the 2nd. If there is no space for the 2nd TextView then it will fall to the next line.

How to add leading white space (spaces) in TextView (Android)

I am trying to add leading white space (spaces) to the string displayed in my TextView when I use setText(), but TextView just removes them from the String.
For example, say I want to display " 328". I call textView.setText(" 328"), but what is displayed in the TextView is "328". The TextView will always display:
328
Instead of:
328
Please keep in mind that I must solve this while using .setText() and not through resources. I have also tried prepending it with "\u00A0","\u0020", and "&#160" but they don't work.
use this below method to add space before text.
textView.setText(" "+"328");

How to truncated TextView without using Single line = true?

I have a textView where I am getting data from JSON and displaying. My problme is that I want to store all data into database but want to display few letters on TextView. I used
android:singleLine = "true"
Is there any way to show only few characters in textView but getting full data in background ?
You can use in xml android:maxLength="10"
You can use android:maxLength="10" (for showing only 10 characters). Apart from android:singleLine = "true" if you want to show 3 dots at the end of the text then you can use android:ellipsize="end"
To store the full text data in database , store the json string which comes from server , however in the textview use maxLength property to restrict the number of characters. Hope this helps.
android:maxLength="<Any character Length>" (for showing only n
characters).
android:singleLine = "true"
if you want to show 3 dots at the end of the text then you can use android:ellipsize="end"
So storing the full text data in database , store the json string
which comes from server , however in the textview use maxLength
property to restrict the number of characters.
Hope this will help you !.
android:singleLine = "true" doesn't have to do anything with the data in background. You will get your data in database as it is.
singleLine will only show the maximum data it can show in a single line on UI.
However, you can use maxLength to get exact maximum characters you want to show, maxLines to set maximum number of lines the TextView can be expanded to and ellipsize to show that there are more words but it can not be fit in a single line.

How to set long text in a textview with a continuation mark at the end of the textview

I have a textview with fixed height. I want to set a long text, but text comes at the last line getting cut by half, I want to avoid it and want to show a continuation symbol. I am giving an image here, I wan to achieve it like in the image.
Use the ellipsize attribute of TextView. By setting :
<TextView
....
android:ellipsize="end">
</TextView>
if the text is longer than your TextView you will have dots(...) at the end of the TextView.
Two line of string only possible to do like this. set ellipsize = end in text view properties.
you should use \n and special symbols from google code.

Categories

Resources