Analog and Digital clock in Android with Example

In Android, Analog and digital clocks are used to show time in android application.

The AnalogClock is a two-handed clock, one is hour indicator and the another one is  minute indicator. The DigitalClock is look like your normal digital which display hours, watch on hand, 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.

Example

Let’s get start by creating a project in Eclipse IDE.

  • Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
  • Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken one Analog and one Digital Clock.

 

<RelativeLayout xmlns:androclass=”http://schemas.android.com/apk/res/android”

xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

tools:context=”.MainActivity” >

<AnalogClock

android:id=”@+id/analogClock1″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_alignParentTop=”true”

android:layout_centerHorizontal=”true”

android:layout_marginTop=”22dp” />

 

<DigitalClock

android:id=”@+id/digitalClock1″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_below=”@+id/analogClock1″

android:layout_centerHorizontal=”true”

android:layout_marginTop=”81dp”

android:text=”DigitalClock” />

 

Guidelines : Learn Android App Development

MainActivity.java

package com.example.analogdigital;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

Output

Analog and Digital clock in Android Example