SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

Android Textview HTML not displaying colors?

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

Upvotes: 0

Views: 1608

Answers (3)

Giorgio Antonioli
Giorgio Antonioli

Reputation: 16214

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

Upvotes: 1

M D
M D

Reputation: 47817

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

Upvotes: 1

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

Upvotes: 0

Related Questions