Android analogclock and digitalclock example

In Android, the AnalogClock is a two-handed clock, one for hour indicator and the other for minute indicator. The DigitalClock is look like your normal digital watch on hand, which display hours, minutes and seconds in digital format.

Both AnalogClock and DigitalClock are UNABLE to modify the time, if you want to change the time, use “TimePicker” instead.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. AnalogClock and DigitalClock

Open “res/layout/main.xml” file, add AnalogClock and DigitalClock in XML.

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Analog Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Digital Clock"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DigitalClock" />

</LinearLayout>

2. Code Code

No idea what can i do with AnalogClock or DigitalClock.

File : MyAndroidAppActivity.java


package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.DigitalClock;

public class MyAndroidAppActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);
		//what can i do with AnalogClock?

		DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
		//what can i do with DigitalClock also? for display only
		
	}

}

3. Demo

Run the application.

1. This is how AnalogClock and DigitalClock look like :

android analogclock and digitalclock demo

Download Source Code

References

  1. Android DigitalClock JavaDoc
  2. Android AnalogClock JavaDoc

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
:D:D:D
11 years ago

you are the beeeest 😛

kiven
12 years ago

this is my code mr. mkyong can you help me please

public class DateTimePickerDialog extends AlertDialog.Builder {

private DatePicker _dpDate;

private TimePicker _tpTime;

//GETTERS

public DateTime getDate(){

//Date picker getMonth is zero base, so we need to add 1 as joda datetime accepts 1-12

return new DateTime(_dpDate.getYear(), _dpDate.getMonth() + 1, _dpDate.getDayOfMonth(), _tpTime.getCurrentHour(), _tpTime.getCurrentMinute());

}

public String getFormattedDate(){

return (String) DateFormat.format(“MM-dd-yyyy hh:mm”, getDate().getMillis());

}

//setters

public void setDate(DateTime dt){

}

kiven
12 years ago

sir mykong can u please help me how to set current date,present date and previous date of my datetimepickerdialog scheduler to my android using jodatime

Mayaa
12 years ago

Thanks for the example, even this http://www.compiletimeerror.com/2013/08/analog-and-digital-clock-in-android.html might help, have a look..

khush joshi
12 years ago

how toopen url in android web browser
i do it but it define error
ple send right java and xml coad

khush joshi
12 years ago

i am make “url is show web site ”
but is no run in my anroid emulator
so what do i
or u send proper coad of “how to create url page “

Owais
13 years ago

Is there any way i can move the hour and minute hands manually?
I actually want to move my minute hand with the value of the coutdown timer in my program. Is there anyway for that?
It would be really helpful, Thank You
Owais Ahmed

Surya
13 years ago

I need source code to display analog clock in one activity and digital clock in one activity..
I mean, if I click 1st button, it should display analog clock.. If I click second button, it should display digital clock..

Norah
12 years ago
Reply to  Surya

Split the layout file into two layouts, one for the analog clock and the other for the digital clock. link them with new activities instead of one.

For example, for the analog clock:

Its activity:

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;

public class analogClock extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);
//what can i do with AnalogClock?

}

}

Tan Viet
13 years ago

How can I customize time format of DigitalClock ? I just want to show hh:mm instead of hh:mm:ss a (default).

William Kinaan
13 years ago

i want to build digital clock that holds just 360 seconds , i mean the user have to answer the question in 360 seconds , what have i write in activity

 
DigitalClock timer;
public void initialized(){
timer=(DigitalClock)findViewById(R.id.dcTimer);
}
 

thank you for helping

Ale
14 years ago

Thanks!!! It help me a lot! =)

vasu
14 years ago

In this example, where we use this ac and dc local variables. while iam running this application on eclipse it will showing an error like ac and dc variables are not used.

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.DigitalClock;

public class MyAndroidAppActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);
//what can i do with AnalogClock?

DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
//what can i do with DigitalClock also? for display only

jagruti
14 years ago
Reply to  mkyong

import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.DigitalClock;

public class MyAndroidAppActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);
//what can i do with AnalogClock?

DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
//what can i do with DigitalClock also? for display only

in this example ac dc variable declare above @overrite in eclipse and then use so error will not come…

johnny crnogorac
13 years ago
Reply to  jagruti

is it possible to have a button that swaps between an analog and digital clock, so that only one is viewed at a time? would appreciate if you could show me some code if possible. thanks

ritesh
13 years ago

yeah… by using intent…we can make another page for digital or analog clock and just make a button on first page for going to second page…by intent..