Question

Can I apply Roboto light and Roboto bold in same TextView on Android 2.3 like that?

**user** has been publish a beez

Where **user** is Roboto bold and has been publish a beez is Roboto light

Was it helpful?

Solution

Yes you can do ..

String firstWord = "user";
String secondWord = "has been publish a beez";

// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif-light",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Set the text of a textView with the spannable object
textView.setText( spannable );

You can use Roboto natively from Android 4.1+ like this:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top