Bookmark hint when typing in EditText on Android

I have an EditText field and the user can enter only six digits. When an EditText focused, it should hint to the user that he can enter only six digits.

The idea is to display the remaining numbers in gray, for example:

Hint for the remaining digits

When the user enters a number, it looks like this:

Hint for the remaining digits

(The red vertical bar is the cursor.)

The cursor should not be located beyond its current position in both images; gray numbers should only be visible and should not be selected.

So actually I want to set the placeholder text, but keep some of the placeholder text while the user types.

I read about the TextInputLayout class from the design support library, but I don't know how I can achieve the above idea.

How to do it?

+5
source share
2 answers

I will give you only an idea, so the implementation is up to you. Create your TextWatcher in

 onTextChanged() 

Count how many digits the user has written and, depending on this number, create a line filled with zeros. After that, make zeros of a different color

 Spannable textWithTintedZeros = new SpannableString(paddedString); textWithTintedZeros.setSpan(new ForegroundColorSpan(yourGrey), firstZeroIndex, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); editText.setText(textWithTintedZeros); 

And finally set the choice to zeros with

 editText.setSelection(indexBeforeFirstZero); 

Remember to also block changing the position of the cursor. I think it can be done with

 View.OnKeyListener 
+2
source

You can add a TextView with gravity right above the text. Then, in the edittext (ontextchanged) listener, change the TextView.

I do not know if you can only do this with EditText.

0
source

Source: https://habr.com/ru/post/1242434/


All Articles