How can I make an always visible, fillable hint in EditText?

Viewed 107

Is it possible to create in EditText something like the image below?

enter image description here

In first case there is no input. In second case input is "1", In third case input is "12", In fourth case input is "123", and so on...

I tried to do this using TextChangedListener, but every time i setText like "0,0" + "1" it repeats reqursively.

2 Answers

You are on the right way. The reason is that you do a setText on the edittext, to avoid recursively calls on your afterTextChanged do:

  1. removeTextChangedListener(yourWatcherObject)
  2. Do your number format operation
  3. addTextChangedListener(yourWatcherObject) again

To make the hint always visible you can use two editext, one over the other and play with the TextWatcher.

Actually you want to use an EditText with multiple colors. You don't need use two or more EditText. You can do that easily using SpannableString object. With SpannableString, you can customize specific parts of your text as you want. ( size, color, type etc. ). You can use setText method of EditText to change text after the customization.

fun getSpannableText(): SpannableString {
    val text = "0,01"
    return SpannableString(text).apply {
        setSpan(ForegroundColorSpan(Color.DKGRAY), text.length - 1, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        setSpan(ForegroundColorSpan(Color.GRAY), 0, text.length -1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    }
}

Result:

Result

Related