In this tutorial, we shall see how to display clocks in Android - Analog as well as Digital. The Analog clock displays two hands- one for hours and the other for minutes, while digital clock shows the hours, minutes and seconds in a digital format.
Note:- Analog and digital clocks can only be used to display time and cannot modify time. If you wish to modify time, use time picker instead.
Note:- Analog and digital clocks can only be used to display time and cannot modify time. If you wish to modify time, use time picker instead.
1. Add the clocks in XML
Open the main.xml file and 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:gravity="center"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Analog Clock"android:textAppearance="?android:attr/textAppearanceLarge" /><AnalogClockandroid:id="@+id/analogClock1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Digital Clock"android:textAppearance="?android:attr/textAppearanceLarge" /><DigitalClockandroid:id="@+id/digitalClock1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="DigitalClock" /></LinearLayout>
2. Java Code
We shall display both the clocks in Java. We are not doing any manipulations out here.File: Main.java
package com.endeavour.tutorial;import android.app.Activity;import android.os.Bundle;import android.widget.AnalogClock;import android.widget.DigitalClock;public class Main extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);AnalogClock ac = (AnalogClock) findViewById(R.id.analogClock1);//what you wish to do with AnalogClock goes here
DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);//what you wish to do with DigitalClock goes here
}}
No comments:
Post a Comment