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
- <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" />
-
- </RelativeLayout>
Activity class
We have not write any code here.
File: 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) {
-
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
Output:
simple and easy topic
ReplyDelete