AdSense

AdSense3

Wednesday, 5 August 2015

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.

activity_main.xml

Now, drag the analog and digital clocks, now the xml file will look like this.
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  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" />  
  14.   
  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" />  
  23.   
  24. </RelativeLayout>  

Activity class

We have not write any code here.
File: MainActivity.java
  1. package com.example.analogdigital;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.   
  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(R.menu.activity_main, menu);  
  19.         return true;  
  20.     }  
  21. }  
Output:
android analog and digital clocks example output 1

1 comment: