Android analog clock example

In android with the help of AnalogClock class it is very easy to display a clock which initially will show the current time and will rotate based on time change. In this tutorial we will learn how to implement Analog clock in android.

It is as simple as adding the following code to your layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_analog_clock">

<AnalogClock
android:id="@+id/an_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>

</RelativeLayout>

Now if you run the application you are supposed to get the following output

analog-clock
analog-clock

You can add the Analog clock to your activity dynamically by using the below code instead of using xml

AnalogClock an_clock = new AnalogClock(this);
yourlayout.addView(an_clock);

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *