0

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?

3 Answers 3

2

For this purpose you need a SpannableString. Here the documentation

Sign up to request clarification or add additional context in comments.

1 Comment

it seems to be we need to do it through java coding is it possible to do in xml level??
1

Try this:

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

1 Comment

u r welcome..you should accept one. as your answer so it help others while finding answer for your question
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.