Analog and Digital Clocks in Android

Introduction

 
This article explains analog and digital clocks in Android. Android Studio is used to create the sample.
 
An analog clock and digital clock displays the time from the current device.To display an analog or digital clock you need to drag and drop the analog and digital clock from the pallet. The analog and digital clock cannot change the time of the device; to do so you will use TimePicker and DatePicker.
 
Step 1
 
Create a project like this:
 
ClockProject.jpg
 
Step 2
 
Create an XML file and with this:
  1. <RelativeLayout xmlns:android="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.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#b76e79">  
  11.    
  12.     <TextView  
  13.         android:gravity="center"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Clocks"  
  17.         android:textStyle="bold"  
  18.         android:textSize="30dp"  
  19.         android:id="@+id/textView" />  
  20.    
  21.     <AnalogClock  
  22.         android:gravity="center"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:id="@+id/analogClock"  
  26.         android:layout_marginTop="50dp"  
  27.         />  
  28.    
  29.     <DigitalClock  
  30.         android:layout_centerInParent="true"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.    
  34.         android:id="@+id/digitalClock"  
  35.         android:layout_marginTop="100dp" />  
  36.    
  37. </RelativeLayout> 
Step 2
 
Create a Java class file with the following:
  1. package com.analogdigitalclock;  
  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.main, menu);  
  19.         return true;  
  20.     }  
  21.    
Step 3
 
Android manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.analogdigitalclock"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.analogdigitalclock.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 4
 
Analog and Digital Clock

ClockImage


Similar Articles