AndroidDev
AndroidDev

Reputation: 16375

Android EditText change color of a single word

How do you go about changing the color of a single word in a EditText ? Meaning all words will be once color and one single word will be a different color.

Kind Regards

Upvotes: 3

Views: 1306

Answers (3)

Feng Dai
Feng Dai

Reputation: 633

Use ForegroundColorSpan

SpannableStringBuilder ssb = new SpannableStringBuilder(yourText);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(
        context.getResources()
        // Specify your color
        .getColor(R.color.your_font_color));
realPrice.setSpan(colorSpan,
         0, // Start index of the single word
         1, // End index of the single word
         Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourEditText.setText(ssb);

Upvotes: 3

Remees M Syde
Remees M Syde

Reputation: 2609

Try this.

String text = "<font color=#0000ff>sample</font> <font color=#ff0000>text</font>";
    edittext.setText(Html.fromHtml(text));

Upvotes: 1

khurram
khurram

Reputation: 1362

Yes you can do this like below image

enter image description here

You can implement this for EditText also. See here full details tutorial.

I hope this will solve your problem.

Upvotes: 1

Related Questions