Initiate/Display an Analog Clock in Android (With Codes)

Android | Display an Analog Clock: In this tutorial, we will learn how to work with a pre-defined Analog clock in Android. How you can display an Analog clock in Android? By Manu Jemini Last updated : June 06, 2023

Display an Analog Clock in Android

Analog Clock is a very easy way to show the current time in the form of a clock. Analog Clock provides very simple techniques to set it. For this tutorial to work, just create a new project with any name. You will get a default activity namely 'MainActivity', you can make use of it.

Starting with your XML file, drag and drop it from the widget section. There is no implication about the parent layout type. Make your layout the way you want it.

The second thing is your JAVA file. To begin with, you should import Analog Clock from the widget, this is done by adding this line import android.widget.AnalogClock;. After that, in your activity, there would be a default onCreate() function, this would be pre-set to use quickly.

Now, you would need to have a variable of type AnalogClock and set it with the reference of the AnalogClock in the layout file.

  • AnalogClock analogClock;
  • analogClock=(AnalogClock)findViewById(R.id.analogClock);

Above Line would do it. After doing everything up, your code should look like this. Connect an Android device or run an ADV and then build your project to see the result

Example to Display an Analog Clock in Android

Java file

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AnalogClock;


public class MainActivity extends AppCompatActivity
{
    AnalogClock analogClock;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        analogClock=(AnalogClock)findViewById(R.id.analogClock);

    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <AnalogClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/analogClock"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp" />
    
</LinearLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Analog clock in Android


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.