How to Set a Clock Through Programming in Android

How to set a clock through programming in Android.

Procedure

  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml file for layout design.
  5. In Linear Layout take two layouts one is for analog clock and another is for digital clock.

Like this:

<AnalogClock

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<DigitalClock

android:layout_width="wrap_content"

android:layout_height="wrap_content" />  

Code is given below:

MainActivity.java

package com.example.myclock;

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.main, menu);

       return true;

    }

}

Activity_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:orientation="vertical">

<AnalogClock

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<DigitalClock

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</linearlayout>  

Output

 
Ebook Download
View all
Learn
View all