I can't find a way to set the text of an EditText
in a Material TextInputLayout
to a SpannableString
and have the styling actually show up on screen. I've tried every combination of EditText
inside a TextInputLayout
, e.g.:
AppCompatEditText
EditText
TextInputEditText
with every way to create a SpannableString
, e.g.:
HtmlCompat.fromHtml(string, HtmlCompat.FROM_HTML_MODE_COMPACT)
SpannableString(string).setSpan(...)
TextUtils.stringOrSpannedString(string)
with every way to set the text of an EditText
, e.g.:
edittext.setText(spannedString)
edittext.setText(spannedString, TextView.BufferType.SPANNABLE)
edittext.append(spannedString)
The crazy thing is they all work when the EditText is not inside a Material TextInputLayout
, so it must be a problem with that. More information: the SpannableString
is respected when you set the hint or placeholder text on the TextInputLayout
, just not the text of the EditText
inside it. I looked through the TextInputLayout
source code, but didn't see anything wrong. Thanks to anyone who can help.
I figured it out right when I was going to give up. You can get the Editable
object from the EditText
and then apply the Span
to it. Here's how:
mEditText.text.setSpan(...)
Surprisingly easy when you know where to look.