6

How I can set custom font in my widget ?

remoteViews.setTextViewText(R.id.text1,""+ days);

Can someone show me an example of how to set the font for this?

Goliathus
  • 77
  • 1
  • 3
  • Look at that URL [custom-font-on-a-android-widget][1] [1]: http://stackoverflow.com/questions/7376325/custom-font-on-a-android-widget – Talha Nov 15 '12 at 17:59

3 Answers3

11

How I can set custom font in my widget ?

You don't. You can choose one of the three built-in typefaces in your layout XML via android:typeface, but you cannot use a custom font with a TextView in a RemoteViews. setTypeface(), the Java method for specifying a custom font on a TextView, is not available for RemoteViews.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • you mean there is no solution to use my custom font in the custom notification layout? I've seen in some apps there is a custom font in the notification – hassan moradnezhad Jul 10 '20 at 09:17
  • 1
    @hassanmoradnezhad: Then they are rendering that text in that font to a bitmap and are displaying the bitmap. This is not a good choice from an accessibility standpoint. – CommonsWare Jul 10 '20 at 10:36
  • thanks, I found [this article](https://www.spaceotechnologies.com/custom-font-notification-title-remoteviews/) which helped me – hassan moradnezhad Jul 11 '20 at 06:04
1

There are 2 workarounds that may help.

1 - If you are able to display only text that the AppWidget sets itself, you can use SpannableStrings and TypefaceSpans to do this (and Stylespans for bold, etc.) You can set a SpannableString using the normal setTextViewText call.

2 - If you actually need to change the TextView's font family so that future text shows up with the font family, this is harder. As mentioned by CommonsWare, you cannot use RemoteViews to set an arbitrary font family programmatically. However, there is a way to do this if you have a smaller set of pre-defined fonts (or font weights like bold, etc.). It ain't pretty, but it works if the number of fonts and variations are fairly controlled.

In my case, I was creating a clock appwidget using TextClock. Because the text updates are not controlled by the AppWidget, I could not use SpannableStrings. I wanted to provide 6 built-in font options. I created 6 TextClocks in my layout file, each with a different font, and simply make the one I want visible and the others "gone" at configuration time. Ugly but effective. So:

appwidget_layout.xml

<TextClock
    android:id="@+id/clock_seconds_condensed"
    android:fontFamily="sans-serif-condensed"
    ....
/>
<TextClock
    android:visibility="gone"
    android:id="@+id/clock_seconds_normal"
    android:fontFamily="sans-serif"
    ....
/>
<TextClock
    android:visibility="gone"
    android:id="@+id/clock_seconds_black"
    android:fontFamily="sans-serif-black"
    ....
/>
...

configurationActivity

//Which font do we want? Choose that clock
val clockid = when (fontFamily) {
    "san-serif" -> R.id.clock_seconds_normal
    "sans-serif-light" -> R.id.clock_seconds_light
    "sans-serif_black" -> R.id.clock_seconds_black
    "sans-serif-thin" -> R.id.clock_seconds_thin
    "sans-serif-medium" -> R.id.clock_seconds_medium
    else -> R.id.clock_seconds_condensed
}

//Hide all the other clocks
if (clockid != R.id.clock_seconds_normal) views.setInt(R.id.clock_seconds_normal, "setVisibility", View.GONE)
if (clockid != R.id.clock_seconds_light) views.setInt(R.id.clock_seconds_light, "setVisibility", View.GONE)
if (clockid != R.id.clock_seconds_black) views.setInt(R.id.clock_seconds_black, "setVisibility", View.GONE)
if (clockid != R.id.clock_seconds_thin) views.setInt(R.id.clock_seconds_thin, "setVisibility", View.GONE)
if (clockid != R.id.clock_seconds_medium) views.setInt(R.id.clock_seconds_medium, "setVisibility", View.GONE)
if (clockid != R.id.clock_seconds_condensed) views.setInt(R.id.clock_seconds_condensed, "setVisibility", View.GONE)

//Set clock parameters
views.setInt(clockid, "setVisibility", View.VISIBLE)
....
Emilie
  • 265
  • 1
  • 9
-3

Try this, It might Help You to understand how to apply custom font effects for your textview

Definitely you wil get Idea using this Example

RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84