How to use custom designed numbers in your android widget


SDK Version: 
M3
If you don't want to download external fonts into your application, this article will be useful to you. You can draw your own numbers for example in gimp from 0 to 9. Copy thoose numbers in to your project folder's res/drawable subfolder. Each number is a new png (for example: one.png, two.png...). Then don't forget to refresh this directory in eclipse.

In my last project I wrote an easy method, what I used to draw single numbers on a widget.

  1.         /**
  2.          *
  3.          * @param context
  4.          *            - you need a context to draw the number
  5.          * @param numberInt
  6.          *            - the number, what you want to draw (0-9)
  7.          * @param where
  8.          *            - viewId
  9.          * @param remoteView
  10.          *            - RemoteViews type object, at instantiate you need to select a
  11.          *            xml (for example:
  12.          *            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain);
  13.          *             )
  14.          */
  15.         private void intToImage(Context context, int numberInt, int where,
  16.                         RemoteViews remoteView) {
  17.                 if (numberInt < 0 || numberInt > 9) {
  18.                         Log.d("Tutorial", "Invalid number" + numberInt);
  19.                 } else {
  20.  
  21.                         int whereArray[] = { R.id.third, R.id.second, R.id.first }; // viewId
  22.                         int images[] = { R.drawable.zero, R.drawable.one, R.drawable.two,
  23.                                         R.drawable.three, R.drawable.four, R.drawable.five,
  24.                                         R.drawable.six, R.drawable.seven, R.drawable.eight,
  25.                                         R.drawable.nine }; // srcId
  26.                         remoteView
  27.                                         .setImageViewResource(whereArray[where], images[numberInt]);
  28.                 }
  29.         }
  30.  
  31. }

Later you can use that like this:

  1.                 [...]
  2.                 int daysleft = (int) days;
  3.  
  4.                 intToImage(context, (int) Math.floor(daysleft / 100), 0, remoteViews);
  5.                 daysleft = daysleft - (int) Math.floor(daysleft / 100) * 100;
  6.                 intToImage(context, (int) Math.floor(daysleft / 10), 1, remoteViews);
  7.                 daysleft = daysleft - (int) Math.floor(daysleft / 10) * 10;
  8.                 intToImage(context, daysleft, 2, remoteViews);
  9.                 [...]

That's it. Not a big thing, but I hope you found this article helpful.