1

I understand that to use custom fonts in a widget I need to render the font to a bitmap, but how do I set a system typeface programmatically?

android:typeface="serif"

in XML is successful, but I cannot find a way to do this at run time.

I currently have

int fn = fullDate.length();
SpannableString dt = new SpannableString(fullDate);
dt.setSpan(new StyleSpan(Typeface.BOLD), 0, fn, 0);
remoteView.setTextViewText(R.id.dateText, dt);

Which works, but using, for example, Typeface.SERIF, gives The constructor StyleSpan(Typeface) is undefined

spatulamania
  • 6,613
  • 2
  • 30
  • 26
aperture
  • 2,905
  • 3
  • 35
  • 58

1 Answers1

0

The constructor for StyleSpan that you're using takes in a constant defined in Typeface and not one of the fields. The constants available are

Typeface.BOLD
Typeface.BOLD_ITALIC
Typeface.ITALIC
Typeface.NORMAL

If you want to set the typeface to a specific font, you need to use TextView.setTypeFace()

EDIT This cannot be done for widgets since you only have access to the remote view. Others have circumvented this by passing an image to the remote view. See this question for details: How to use a custom typeface in a widget?

Community
  • 1
  • 1
spatulamania
  • 6,613
  • 2
  • 30
  • 26
  • This is for a widget so i don't have setTypeFace() available – aperture Oct 10 '11 at 01:01
  • @aperture For widgets, see this question http://stackoverflow.com/questions/4318572/how-to-use-a-custom-typeface-in-a-widget – spatulamania Oct 10 '11 at 01:03
  • I was under the impression that was only necessary for custom typefaces, not system typefaces – aperture Oct 10 '11 at 01:15
  • The problem is that for a RemoteView, you don't have direct access to the TextView. Since you can specify the resource Id, you can set the typeface in the Xml but there is not way to do it programmatically. – spatulamania Oct 10 '11 at 01:26
  • Ah, so I will change which layoutId the RemoteView uses programmatically then. – aperture Oct 10 '11 at 01:36