SpannableStringBuilder sb = new SpannableStringBuilder("Hello World");
ForegroundColorSpan fcs = new ForegroundColorSpan(R.color.text_blue);
sb.setSpan(fcs, 5, 11,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
res/Values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_blue">#FF39ACEE</color>
</resources>
The color has changed but to something else, rather than the blue color I wanted.
Thank You.
Try this
SpannableStringBuilder sb = new SpannableStringBuilder("Hello World");
int color = getResources().getColor(R.color.text_blue);
ForegroundColorSpan fcs =new ForegroundColorSpan(color);
sb.setSpan(fcs, 0, sb.length(),0);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(sb);
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_blue">#FF39ACEE</color>
</resources>
Snap
The below did not work
ForegroundColorSpan fcs = new ForegroundColorSpan(R.color.text_blue);
sb.setSpan(fcs, 0, sb.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Some time you will use more than one span, they may influence each other. you should set ForegroundColorSpan at last.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With