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?
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?
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
.
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)
....
Try this, It might Help You to understand how to apply custom font effects
for your textview