Update Time And Date Using Button in Android

Introduction

In this article I explain the time and date classes in Android, how to use to the Date and Time Classes in Android and how to work with these classes. You can easily understand it using the following instructions.

Step 1

As usual create a new project file as in the following.

newTimeUpdater.jpg

Step 2

Open the "activity_main.xml" file and update it with the following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:text="@string/update_btn" />

</RelativeLayout>

Step 3

Open the "MainActivity.java" file and update it with the following code:

package com.example.androidthirdapp;

import java.util.Date;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity {

      Button btn;

      @Override

      protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            btn = (Button) findViewById(R.id.button1);

            //Onclick listener

            btn.setOnClickListener(new View.OnClickListener() {

            //Onclick method

            public void onClick(View v) {

            //Call updateTime() when button is clicked

            updateTime();

            }

            });

      }

      //Method definition starts here with no return type

      public void updateTime() {

      //Setting button text with current time

      //java.util.Date() method is used to get the current time

      btn.setText(new Date().toString());

      }

      @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;

      }

}

 

Step 4

Now create a new Java file named "CurrentTimeActivity.java" and use the following code in it:

package com.example.androidthirdapp;

import java.util.Date;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class CurrentTimeActivity extends Activity {

      Button btn;

      public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            btn = (Button) findViewById(R.id.button1);

            updateTime();

            btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

            updateTime();

            }

            });

            }

      //Method definition starts here with no return type

      public void updateTime() {

      //Setting button text with current time

      //java.util.Date() method is used to get the current time

      btn.setText(new Date().toString());

      }

}

Step 5

Open the "AndroidManifest.xml" file and update it with the following code:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.androidthirdapp"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.androidthirdapp.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

Step 6

See the output.

Update Button

updateTime.jpg

Current Time

currentTim.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all