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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
:D:D:D
Guest
:D:D:D

you are the beeeest :P

kiven
Guest
kiven

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
Guest
kiven

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
Guest
Mayaa

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
Guest
khush joshi

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

khush joshi
Guest
khush joshi

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
Guest
Owais

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
Guest
Surya

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
Guest
Norah

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?

}

}

trackback
Android Followers » Android Tutorial From mkyong.com

[…] Android analogclock and digitalclock example Use “AnalogClock” and “DigitalClock” to render a clock like component, which supports hours, minutes and seconds. […]

Tan Viet
Guest
Tan Viet

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

William Kinaan
Guest
William Kinaan

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
Guest
Ale

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

vasu
Guest
vasu

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