Android String color manipulation

I have one text view in android which text is "Hi how are you?"

My query is can we keep color of "hi how" as blue and "are you?" as red in one line as below

See this image

cause now am using 2 text view to show "hi how" and "are you" respectively but i want to show it in one text view with above requirement?

can we give two color code at a time to one text in String file? any reference?

Upvotes: 0

Views: 82

Answers (3)

duggu
duggu

Reputation: 38419

try below code:-

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("hi how are you?");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

Upvotes: 1

jyomin
jyomin

Reputation: 1967

Try this:

String text = "<font color=#0000FF>Hi How</font> <font color=#ff0000>are you</font>";
mTxtVw.setText(Html.fromHtml(text));

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

For this purpose you need a SpannableString. Here the documentation

Upvotes: 2

Related Questions