Pages Navigation Menu

Coding is much easier than you think

How To Create Analog And Digital Clocks in an Android Application using Eclipse:

analog and digital clock

In this tutorial, you will be learning How to display Toggle Button in an activity

Here you go!!!!



Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.
** UPDATE: Android Complete tutorial now available here.

Key terms involved in this project:

View: It is a data structure whose properties store the layout and content for a specific rectangular area of the screen. A View object handles measuring, its layout, drawing, focus changes, scrolling, and key/gestures for the screen area it represents. The View class serves as a base class for all widgets. The list of widgets available includes i.e. TextView, EditText, Button, RadioButton, Checkbox, ScrollView, etc.,

Analog clock & Digital Clock: Analog clock widget displays an analogic clock with two hands for hours and minutes. Digital clock widget displays an digital clock. You can set the width and height of the clock using the attributes available under AnalogClock and DigitalClock to suit your layout/ UI screen.

The tutorial is about how to an analog and a digital clock in the UI of the android application.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.


Now let’€™s go to the coding section. This project requires following files.

Source Code:

  1. – Activity (main screen)

Activity Files:

  1. activity_clock.xml – main screen/ layout

res – Values:

  1. strings.xml – strings available in layout/activity

Manifest file:

  1. AndroidManifest.xml

Here are the coding for the above files.

package com.example.clock; // Clock package for analog and digital clocks in this project
import; // Required to create an activity.
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import android.widget.AnalogClock;  // Required to create an analog clock
import android.widget.DigitalClock;  // Required to create an digital clock
 public class Clock extends Activity {
	public void onCreate(Bundle savedInstanceState) {  // all classes should extend activity
		super.onCreate(savedInstanceState); // Create a new activity
		setContentView(R.layout.activity_clock);// Displays the main screen for Clocks
		AnalogClock analog = (AnalogClock) findViewById(;
		//analog clock
 		DigitalClock digital = (DigitalClock) findViewById(;
		//digital clock


<?xml version="1.0" encoding="utf-8"?>

<!-Setting the linear layout with vertical orientation-->

<LinearLayout xmlns:android=""
    android:orientation="vertical" >
<!-- Displays text Analog Clock on the screen -->
        android:text="Analog Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Analog Clock -->
        android:layout_height="wrap_content" />
<!-- Displays text Analog Clock on the screen -->

        android:text="Digital Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Digital Clock -->

        android:layout_height="wrap_content" />

Menu : activity_clock.xml

<menu xmlns:android="">

Note: Since there are no menu settings involved in this project, delete the contents of the menu settings. This may cause errors in while building the project.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="8" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Strings. xml:


<?xml version="1.0" encoding="utf-8"?>

<!--Application Name-->

    <string name="app_name">Clock</string>

Styles. xml:

<!-- specify properties such as height, padding, font color, font size, background color -->

        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    <style name="AppBaseTheme" parent="android:Theme.Light">
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->


Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

To test your application, unlock the screen and double click on your app.

You can see your app with the image set during the android project creation in AVD.

AVD Home screen_8

Now the activity created for analog and digital clock is shown as in the figure below.

Analog and Digital Clock

Thus the android application project is executed successfully.

About Gokul

I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

  • Sudipto

    Thanks Gokul for sharing this tutorial with us. You explained very well.