Measure text length before rendering - android

I have a widget where I need to display some text on one row. If the text does not fit, I would like to show as much as possible and end the text with "..." to show that not all text is displayed. Is there a way to discover how long the displayed text will be before rendering the widget, so that I can replace the last part of the text with "..."?
Cheers,

You don't need to do that - a TextView can do it for you.
myTextView.setSingleLine(true);
myTextView.setEllipsize(TextView.TruncateAt.END);

Related

Placeholder hint while typing in EditText in Android

I have an EditText field and the user may only input six digits. When the EditText is focused, it should hint the user that he can only input six digits.
The idea is to display the remaining digits in gray, like this:
When the user inputs a digit, then it appears like this:
(The red vertical bar represents the cursor.)
The cursor should not be positioned behind its current position in both images; the gray digits should only be visible, and should not be selectable.
So in fact I want to set a placeholder text, but retain a part of the placeholder text whilst the user is typing.
I read about the TextInputLayout class from the Design Support library, but I don't know how I can achieve abovementioned idea.
How to do this?
I will give you only the idea, so implementation is up to you. Create your TextWatcher, in
onTextChanged()
Count how many digits user wrote and depending on this number create string padded with zeros. After it, make zeros be of different color
Spannable textWithTintedZeros = new SpannableString(paddedString);
textWithTintedZeros.setSpan(new ForegroundColorSpan(yourGrey), firstZeroIndex, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
editText.setText(textWithTintedZeros);
And at last set selection to be before zeros with
editText.setSelection(indexBeforeFirstZero);
Don't forget also to block changing cursor position. I think can be done with
View.OnKeyListener
You could add over the edittext a TextView with gravity right. Then in the listener of edittext (ontextchanged) modify that TextView.
I don't know if you can do it with only EditText.

Empty BulletSpan issue in android Edittext

I'm implementing a rich text editor. In that, when user clicks on bullet icon in toolbar, an empty bullet should be created with no character in it. But I can not achieve this by setting span on the cursor position. I have to add an unwanted space to do this. Using below code,
editable.insert(cursorPos, " ");
editable.setSpan(new BulletSpan(), cursorPos, cursorPos + 1,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
I have also tried with LeadingMarginSpan but same result. I am getting the required feature, if the cursor is placed in first position of EditText or there is any text in that line. Also same problem persists in Numbered list also.
And the problem persists only for first bullet/Number in list. In subsequent bullet or numbered list item the span is working properly.
Can you please anyone help me how to implement empty bulletspan without any space or character ?
Thanks in advance...

Detecting click event on a particular text in TextView with marquee

I am setting a text in textview.And it is marquee text.Not just the text, it is an array of text concatenated one after another and added to textview.I want that if i will click any text then that text(or word) will be shown in a dialog box.First of all is it Possible anyhow.I tried to use Spannable from this link first answer.but it's not working as per my requirement as i have marqueeing text.
Why wouldn't you replace the marque with something more useful? :) You may wrap your textView into a horizontal scroll view. Then you have a thread (AsyncTask) that scrolls the text, using ScrollView.setScrollX() just as marquee does.
Can you guess, what comes next? Use spans, as suggested by the link, you quoted!

Android accessibility feature for TextView

Hi I have a view with a TextView and linear layout. Linear layout is a kind of custom keyboard with number of buttons. So every button click I have to do some validations and append a character to the TextView.
Click of each button I append the character to the textview. Now when I turn the Accessibility feature ON, it is expected to read out the last character that is entered. But what happens is every time I set text in the text view.. Accesibilty feature reads out the 1st char in the textview, not the last char..
so Im confused on how to control the accessiblity of textview.. Can anyone plz tell me how to control the accessibilty.
You could use the setContentDescription() on your TextView to set it to the character you just entered. Then when accessibility kicks in it will ask your TextView for its content description and it should work. See docs here:
https://developer.android.com/reference/android/view/View.html#setContentDescription(java.lang.CharSequence)

Android Text not fitting into the List

I make a simple list choice multiple selection option, using an array of String. The text of the list item is very long and wrapped into 2-5 lines. The text is not being fit into the list item boundary if it exids more than 2 rows. The boundary of the List item overlapping the text.
Please let me know how to adjust the height of the List item accoding to the lenght of the text.
Thanks
You need to cut off the text if its very long or wrap everything by adjusting the font?
If you need to adjust with the font you may look into the following question, which I faced and the answer is written by me itself in it...
TextView text shrink to the given width

Categories

Resources