How to add leading white space (spaces) in TextView (Android) - 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");

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

How to avoid text wrapping in multiline TextView between two words?

I would like to display some text in a TextView, but I would like to avoid text wrapping in a specific position of the text, to avoid the text to display like this:
This is my very long text
:
Instead, I want my text to display as following:
This is my very long
text :
You may ask: why is there a space before :?
Simply because it's in French, and, unlike English, you're supposed to add an extra space before this character (also available with ? and !).
I tried this:
TextView myTextView = (TextView) findViewById(R.id.my_text);
myTextView.setText(Html.fromHtml("This is my very long <span style=\"white-space: nowrap;\">text :</span>"));
But of course, it doesn't work.
I was also thinking of a specific character that behaves like a space but without wrapping. I don't know if it exists, and if it's a good practice.
I finally found out a solution for the specific white space wrapping problem: just add \u202F instead of your white space in your string, like for example:
<string name="my_string">This is my very long text\u202F:</string>

How to prevent automatic TextView word wrapping while allowing manual word wrapping

I have a string with several \n line breaks in it:
'abc \n abcabcabc abcabc \nabcabc
I want my TextView to show the string as is, with exactly those line breaks that are in the string. The TextView should not add additional line breaks, and it should not remove any.
I'm using the following:
textView.setMaxLines(4);
textView.setSingleLine(false);
but the TextView renders the second line split into two lines since it is too long to be displayed in a single line.
Removing setSingleLine(false) leads to the TextView rendering everything in one line.
Setting setMaxLines(3) leads to the TextView rendering only the first two lines (on three lines). The fourth line is not rendered anymore.
Any hints?
try just using html and forgo using setMaxLines or setSingleLine :
TextView foo = (TextView) findViewById(R.id.yourTextViewId);
foo.setText(Html.fromHtml("<nobr>abc </nobr><br/><nobr> abcabcabc abcabc </nobr><br/><nobr>abcabc</nobr>"));

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.

TextView setText problem

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.

Categories

Resources