0

I have TextView. Using java code I want to set color for string variable that I have to append to the text. Variable is generated at run time.

I explored Spannable but you have to give start and end which is not fixed. Any other way to fix this. Please help.

Code:

String text; (Filled at runtime)

//but I want it to be different color
textview.append(text);

2 Answers 2

4

You can do something like this to set the text in your TextView:

tv1.setText(Html.fromHtml("<font color='red'>R</font><font color='green'>G</font><font color='blue'>B</font>"));
Sign up to request clarification or add additional context in comments.

2 Comments

As i have mentioned string is got at runtime and the method you have specified works for static strings if am nt wrong
Not really, you can concatenate or replace the strings: "<font color='red'>" + text + "</font>..."
0

The issue with this is that when you have just one text object you can only have one colour for it. You will have to use more then one text object (each with different colours) and juxtapose them in you design.

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.