I'm trying to create a digital clock widget with a custom font. And this has proven to be the biggest challenge of my Android experience. (Thought it would be as simple as tc.setTypeFace("whatever")
and be done with it)
The best way seems to be scratch the TextClock
and use an ImageView
and pass a Bitmap to it using a custom AlarmManager
to create a new image for the view each second.
Before I get into the Bitmap generation, I practiced on a simple TextView
using this tutorial Alarm Manager Widget
My problem is I can't this to update every second. I use
am.setRepeating(AlarmManager.RTC_WAKEUP , System.currentTimeMillis()+1000, 1000, pi);
but it still only updates it every minute or so (not at the top of every minute though). I think this has something to do with the way more recent OS (Kitkat, Lollipop) handle AlarmManagers
.
My questions are:
Is this the right way to go about doing this? If not, please explain what I should do.
How can I get the widget to update every second?
Digital clock widget comes to your Android phone to display the time and date clearly on your background. This clock gives you an alternative look for Android, letting your phone stand out amongst the rest.
I had the same problem... You have 2 options as I know till now:
android:fontFamily="sans-serif-thin"
attribute for example in your xml file.
Works like a charm!This is my TextView:
<TextView
android:id="@+id/Time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:fontFamily="sans-serif-thin"
android:gravity="center_vertical|center_horizontal"
android:includeFontPadding="false"
android:singleLine="true"
android:text="12:01"
android:textColor="#fff"
android:textSize="80sp" />
And this is how it looks like:
The only disadvantage of this method is that there isn't large selection of system fonts you can use.
Edit: For updating the widget every minute - you need to start a Service which can update your clock each minute (or second if you want). Check this example too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With