Sup everyone! Happy Holiday to all.This time r2d is back with another cool android project. We are going to make a digital clock widget of our own. Isn't that cool!

Note that Android has an in-built Digital Clock widget in its layouts , but we want more customization so we are implementing our own version of it. So let's roll.

1. Create a new Android Application Project

  • Go to File > New > Android Application Project.
  • Complete the wizard and create a new Activity (
  • In the activity_main.xml file under res/layout, create two TextView s to be used to show time and date.
  • The code and the resulting layout will be like this-

Layout of the widget

2. Add a new AppWidgetProvider resource to res/xml

  • Create the res/xml folder if not available already.
  • Add a new xml to this folder and name it widget_xml.xml (or anything you like).
  • Add the code given below to the xml file.The Structure is also shown if figure below.
  • The fields are as described below:
  1. Min Width/Height - min. width/height with which the widget will be drawn.
  2. Min. Resize Width/Height - width/height to which it can be re-sized.
  3. Initial layout - the layout which is used to draw the widget.
  4. Resize Mode -  direction in which re-size is allowed.

Fields in the AppWidgetProvider

3. Make changes to the Manifest XML

  • Add the following permissions the manifest file as they will be used to access some features of Android system later while coding.

<uses-permission android:name="" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
  • Remove the <activity> tag from your  <application> tag as our app doesn't implement an activity.Instead add a receiver for the widget as shown below:-
Final view of the Project Explorer

4. Code the MainActivity

  • The MainActivity extends AppWidgetProvider so that it can use the services of the Android API.
  • The various methods such as OnReceive(), OnUpdate() , OnEnabled(), OnDisabled() of the AppWidgetProvider are overridden , to apply widget life cycle methods.
  • More in the last section.

--Code Explained --

  1. The OnReceive() method is called when the widget receives call to update its contents. Here we get all the id(s) for all the instances of our widget , and call updatetime() method the we defined, to update time in every instance.
  2. The createClockIntent() method is used by the AlarmManager to send a pending Intent to our widget every 1000 msec. The Android system only allows to update a widget every 30min (due to several reasons). So we use Alarm Manager to update it each second.
  3. The OnEnabled() method is used to start the Alarm Manger and the method OnDisabled() is where the timer is disabled.
  4. Finally in the OnUpdate() method we acquire a Wake Lock, to keep the CPU on (Note that this is not necessary here ,but I thought to show it's use here). the it uses RemoteViews to send Pending Intent to each TextView to open ALARM Intent. Finally the Wake Lock is released. 
  5. The updatetime() method is just filling the TextViews with the required time and date.

Download Code



Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image
    Blogger Comment
    Facebook Comment


Post a Comment