Multicolor string (android)

I have a line where I would like the component to be green. Unfortunately, I tried different ways without getting any results. Maybe some of you guys can help me with this.

Here's what I tried: Option 1:

String b = "<font color = '#82FA58'>" + Integer.toString(deal.megadeal) + " " + mStrings.getString(AppStrings.textdeal) + "</font>";
ty.redeemText.setText(a + " " + Html.fromHtml(b)  +  " " + c);

      

Option 2:

setText(a + " " + Html.fromHtml("<font color = '#82FA58'> " + b + "</font>")  + " " + c);

      

+3


source to share


3 answers


You can keep the entire HTML string in one and use BufferType.SPANNABLE.



String b= partOfString + "<font color = '#82FA58'>" + greenstring + "</font>" + otherPartOfString;
yourTextView.setText(Html.fromHtml(b), TextView.BufferType.SPANNABLE);

      

+2


source


Your second option worked for me.

Try to go step by step, just a colored simple line, then add other text or variables.

setText(Html.fromHtml("<font color='#522D03'>Hi</font>"));

      



If that doesn't work, try this:

Html.fromHtml("<![CDATA[<font color='#522D03'>Hi</font>]]>");

      

Let me know if one of them works.

+2


source


You can use SpannableStringBuilder

:

String megaDeal = Integer.toString(deal.megadeal);
String textDeal = mStrings.getString(AppStrings.textdeal);
String leftText = a + " ";
String rightText = " " + c;
String middleText = megaDeal + " " + textDeal;
SpannableStringBuilder span = new SpannableStringBuilder( leftText +  middleText + rightText );
span.setSpan(new ForegroundColorSpan(Color.parseColor("#82FA58")), leftText.length(), leftText.length() +  middleText.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
ty.redeemText.setText(span);

      

+1


source







All Articles