4

Is there a way to set multiple colors to the edittext's hint when wrapped by android.support.design.widget.TextInputLayout without compromising the behaviour of floating edittexts?

like, Headline*

Headline and * with different colored hint

RKS
  • 445
  • 4
  • 14
  • Maybe [this](https://stackoverflow.com/questions/14114113/different-colors-at-edittext-in-android) solution also works for hints. – Matthias F. Oct 25 '16 at 12:26
  • Thank you for your answer. But I want to set hint for TextInputLayout, not for Edittext. As i need floating animation for hint text. – RKS Oct 25 '16 at 12:33
  • Have you tried this using [TextWatcher](https://developer.android.com/reference/android/text/TextWatcher.html) – MashukKhan Oct 25 '16 at 12:52
  • Hey @RKS have you tried my answer? – M_droid Jan 03 '20 at 11:24

4 Answers4

4

Try this! If you want to use it for setText just add BufferType.SPANNABLE (see below)

    String redPart = "Hello";
    String bluePart = "World";

    SpannableStringBuilder builder = new SpannableStringBuilder();

    SpannableString redColoredString= new SpannableString(redPart);
    redColoredString.setSpan(new ForegroundColorSpan(Color.RED), 0, redPart.length(), 0);
    builder.append(redColoredString);

    SpannableString blueColoredString= new SpannableString(bluePart);
    blueColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, bluePart.length(), 0);
    builder.append(blueColoredString);


    myEditText.setHint(builder)

    //do following for setText
    myEditText.setText(builder,BufferType.SPANNABLE)
M_droid
  • 2,447
  • 2
  • 25
  • 35
0

Use SpannableString class which allows you to use different styles on parts of your string ... If I remember correctly, TextAppearanceSpan class is used for coloring a text..

koperko
  • 2,447
  • 13
  • 19
0

See below code this is work for me.

 EditText editText = (EditText) findViewById(textView);
 Spannable wordtoSpan = new SpannableString("Hello world");

 wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 editText.setHint(wordtoSpan);
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
0

You can acheive this by programtically

SpannableString spString = new SpannableString("HEADERLINE*");
    spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 11,     spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    metUserName.setHint(spString);
Rajakumar
  • 907
  • 1
  • 7
  • 17