0

I am using like this it should show green color but showing black

all rgb values are dynamic(web service) same code working in browser(W3 SCHOOLS)

mytextview.setText(Html
            .fromHtml("<font style=\"color: rgb(102, 204, 0);\">REGISTRATION</font>"));
2
  • mytextview.setText(Html .fromHtml("REGISTRATION")); shouldn't show green text... Commented Dec 3, 2014 at 11:36
  • 1
    why people are giving negative votes Commented Dec 3, 2014 at 11:50

3 Answers 3

1

try this

String formattedText = "This is &lt;font color='#659D32'&gt;green&lt;/font&gt;";
textElement.setText(Html.fromHtml(formattedText));

Or another way is using SpannableStringBuilder

  String registration = "REGISTRATION";
  SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(registration);
  ForegroundColorSpan colorSpannable = new ForegroundColorSpan(Color.rgb(102, 204, 0)); 
  spannableStringBuilder.setSpan(colorSpannable, 0, registration.length()-1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
  mytextview.setText(spannableStringBuilder);
Sign up to request clarification or add additional context in comments.

Comments

1

Where are you writing that "REGISTRATION" is green?

String hex = String.format("#%02x%02x%02x", r, g, b);
String html = String.format("<font color='%s'>REGISTRATION</font>",hex);

mytextview.setText(Html.fromHtml(html));

2 Comments

@SHASHIDHARMANCHUKONDA The colour you posted is the equivalent of #66CC00, use that instead of #00FF00
in my html i have rgb values
0
mytextview.setText(Html.fromHtml("<![CDATA[<font color='#659D32'>REGISTRATION</font>]]>"));

Or use SpannableStringBuilder:

String text = "REGISTRATION";
SpannableStringBuilder span = new SpannableStringBuilder(text);
span.setSpan(new ForegroundColorSpan(Color.parseColor("#659D32")), 0,  text.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
mytextview.setText(span);

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.