Analog and Digital

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android Analog clock and Digital clock example

The android.widget.AnalogClock and android.widget.DigitalClock classes provides the functionality to display analog and digital clocks.

Android analog and digital clocks are used to show time in android application.

Android AnalogClock is the subclass of View class.

Android DigitalClock is the subclass of TextView class. Since Android API level 17, it is deprecated. You are recommended to use TextClock Instead.

Note: Analog and Digital clocks cannot be used to change the time of the device. To do so, you need to use DatePicker and TimePicker.

In android, you need to drag analog and digital clocks from the pallet to display analog and digital clocks. It represents the timing of the current device.


Now, drag the analog and digital clocks, now the xml file will look like this.

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=""  
  2.     xmlns:tools=""  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  7.     <AnalogClock  
  8.         android:id="@+id/analogClock1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="22dp" />  
  15.     <DigitalClock  
  16.         android:id="@+id/digitalClock1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@+id/analogClock1"  
  20.         android:layout_centerHorizontal="true"  
  21.         android:layout_marginTop="81dp"  
  22.         android:text="DigitalClock" />  
  24. </RelativeLayout>  

Activity class

We have not write any code here.



  1. package com.example.analogdigital;  


  3. import android.os.Bundle;  

  4. import;  

  5. import android.view.Menu;  


  7. public class MainActivity extends Activity {  


  9.     @Override  

  10.     protected void onCreate(Bundle savedInstanceState) {  

  11.         super.onCreate(savedInstanceState);  

  12.         setContentView(R.layout.activity_main);  

  13.     }  


  15.     @Override  

  16.     public boolean onCreateOptionsMenu(Menu menu) {  

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

  18.         getMenuInflater().inflate(, menu);  

  19.         return true;  

  20.     }  

  21. }  

android analog and digital clocks example output 1