Tuesday , December 3 2024

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.

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.

1) 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>

2) 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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

 

Preview

clocksoutput1

About admin

Check Also

Binding JavaScript and Android Code – Example

When developing a web application that’s designed specifically for the WebView in your Android application, …

Leave a Reply