In TextInputLayout change the color of hint for single letter or alphabet

无人久伴 提交于 2019-12-05 20:32:03

As per the Android Issue Tracker,

Setting the hint to the editText respects the span, but the label does not float on focus.

editText.setHint(hint);

Setting the hint to the textInputLayout ignores the span

textInputLayout.setHint(hint);

TextInputLayout uses internal class CollapsingTextHelper to draw the hint, which does not support spans.

And as they said,

They have passed this defect on to the development team and will update this issue with more information as it becomes available.

I suggest you using just SpannableStringBuilder without Html.fromHtml:

SpannableStringBuilder sb = new SpannableStringBuilder("Hint with first letter black");
CharacterStyle cs= new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.black));
sb.setSpan(cs, 0, 1, 0);
((TextView) findViewById(R.id.description_messages)).setHint(sb);

In this piece of code the first letter becomes black. If you would like it to be bold too, use this:

CharacterStyle cs = new StyleSpan(android.graphics.Typeface.BOLD);

That just puts hint to EditText. There will be no floating text effect.

In TextInputLayout change the color of hint for single letter or alphabet

String firstNameText ="firstName *";
Spanned redStar; 
String html = "<string style="color:firstNameText;">firstName<span style="color:red;">*</span></string>";
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.L) { 
   redStar = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY); 
} else { redStar = Html.fromHtml(html); }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!