PrajaktaParkhade
PrajaktaParkhade

Reputation: 115

how to set font color code in spannable String?

I want to set the color code in a spannable string.

 SpannableString str= new SpannableString("Your new message ");
        str.setSpan(new ForegroundColorSpan("#00ff00", 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

I have tried the above code but it is not working.

Upvotes: 0

Views: 1040

Answers (4)

HendrikFrans
HendrikFrans

Reputation: 347

Your call to ForegroundColorSpan() misses the closing bracket and its argument should be an integer, not a string. A hex integer literal starts with '0x' in Java. So this snippet will make your code work:

new ForegroundColorSpan(0xff00ff00)

Parsing the string also works, of course, but it is not necessary.

Upvotes: 1

user4571931
user4571931

Reputation:

Please try this. It workes for me.

str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.geen_color)), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Upvotes: 1

Tristan N.
Tristan N.

Reputation: 26

Here color is an integer you should use :

Color.parseColor("#ff00ff00") 

Upvotes: 1

Md. Nowshad Hasan
Md. Nowshad Hasan

Reputation: 626

I hope @Tristan's answer is correct. But if still there is a problem then try -

Spannable str= new SpannableString("Your new message ");
str.setSpan(new ForegroundColorSpan(Color.BLUE, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Upvotes: 1

Related Questions