14

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:

  1. Is this the right way to go about doing this? If not, please explain what I should do.

  2. How can I get the widget to update every second?

Parker
  • 8,539
  • 10
  • 69
  • 98
David Small
  • 581
  • 1
  • 10
  • 25
  • As a side note, there is a time tick broadcast that goes out every minute (if my memory doesn't betray me its what TextClock uses under the hood) http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK – JohanShogun Jul 23 '15 at 23:24

3 Answers3

3

I had the same problem... You have 2 options as I know till now:

  1. The easiest - If you are using API level 16 or higher you can simply add 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:

enter image description here

The only disadvantage of this method is that there isn't large selection of system fonts you can use.

  1. The hard way is just to use your own Class, witch extends the TextView object like that and define it instead normal TextView in your xml file. I do not recommend it because it is possible to appear bugs.

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.

Nikolay Hristov
  • 1,602
  • 1
  • 15
  • 22
  • This does not answer the question about how to update the widget every minute. – Reut Sharabani Jul 18 '15 at 14:13
  • I need to use my own font, not the built in ones. So option 1 can be disregarded. In option 2, the "that" hyperlink you made leads to nothing? Was there supposed to be an example there? I look deeper into the sample code you provided too. You say you don' recommend it, but it sounds like it's my only choice. What kind of bugs are you referring to? – David Small Jul 19 '15 at 15:42
  • The link was wrong, sorry for that. It is fixed. I have not tested enough this way, so I can not guarantee whether it is working fine without bugs. – Nikolay Hristov Jul 20 '15 at 13:17
  • What it be possible to just extend the class of a TextClock? – David Small Jul 21 '15 at 03:30
  • 1
    TextClock requiers API level 17 and higher. – Nikolay Hristov Jul 21 '15 at 09:54
  • 1
    As [Nikolai](http://stackoverflow.com/users/2514069/nikolai-hristov) said that you could use TextClock , but if you want to use the same below 17 you could go for this [link](https://github.com/vojtech/android-textclock-backport). And use the following code to give your custom font. myTextClock.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/font.ttf")); – avinash Jul 22 '15 at 11:26
  • After a lot of tests. The correct way seems to be using a service to update a bitmap every minute. The example you provided @NikolaiHristov was a great help! – David Small Sep 06 '15 at 23:33
3

Use this link to get Textclock below API level 17. You could use .setTypeface(Typeface.createFromAsset(getAssets(),fonts/font.ttf")); with it.

avinash
  • 1,744
  • 24
  • 40
2

For API 17 or higher to customize fonts, you need to download .ttf file for any fonts you want then create a directory assets/fonts/ then save your fonts

enter image description here

Typeface fonttype
fonttype = Typeface.createFromAsset(getActivity().getAssets(), "fonts/robotoregular.ttf");
textclock.setTypeface(fonttype);
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54