Alex Kombo
Alex Kombo

Reputation: 3356

changing font color of part of textview that uses a custom typeface

I have implemented a custom typeface throughout my app using the uk.co.chrisjenx:calligraphy:2.1.0 library.

I now need to change the color of part of a textview. I have tried to use

String username = "<font color='#FC195A'>" + post.getUsername() + "</font>";

but the font color remains the same.

Is there a workaround for this?

Upvotes: 1

Views: 893

Answers (2)

RodneyO
RodneyO

Reputation: 125

After declaring the typeface as shown below

// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf"));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(sBuilder, TextView.BufferType.SPANNABLE);

add the set text color option

setTextColor(Color.BLACK);

Upvotes: 0

user5716019
user5716019

Reputation: 598

You can use Spannable String to get your thing done.

Just follow below link :

Set color of TextView span in Android

To provide color to the username, first get the length of it and do as below :

Spannable wordtoSpan = new SpannableString(username);        
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, username.length();, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
usernameTextView.setText(wordtoSpan);

Upvotes: 1

Related Questions