Question

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?

Was it helpful?

Solution 2

Try this:

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

OTHER TIPS

For this purpose you need a SpannableString. Here the documentation

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top