Tutorials  /  JavaScript

Optimal Utilization of Date and Time Pickers in Android Applications

Ccentron Redaktion · July 2024 ·3 min read ·JavaScript, Tutorial

Contents

  1. DatePickerDialog and TimePickerDialog in Android
  2. Example Project Structure for Android Date Time P…
  3. Android Date Time Picker Dialog Project Code

Date and Time Pickers are commonly used elements in Android apps when it comes to

allowing users to select dates and times. In this tutorial, we demonstrate the usage of a

Date Picker and a Time Picker dialog in our Android application. These components

provide a custom interface for selecting date and time. We will utilize the

DatePickerDialog and TimePickerDialog classes along with the Calendar class in our

Android application code to achieve this.

DatePickerDialog and TimePickerDialog in Android

Although a Date Picker and a Time Picker can be used as independent widgets, they occupy a

significant amount of space on the screen. Therefore, it's better to use them within a dialog.

Fortunately, Android provides its own classes DatePickerDialog and TimePickerDialog.

These classes feature callback methods onDateSetListener() and onTimeSetListener(),

which are called when the user sets the date or time, respectively. The DatePickerDialog

class consists of a constructor with five arguments listed below:

  1. Context: It requires the application context.
  2. Callback function: onDateSet() is called when the user sets the date, with the

    following parameters:

    • int year: It stores the currently selected year from the dialog.
    • int monthOfYear: It stores the currently selected month from the dialog.
    • int dayOfMonth: It stores the currently selected day from the dialog.
  3. int mYear: It displays the current year visible when the dialog is displayed.
  4. int mMonth: It displays the current month visible when the dialog is displayed.
  5. int mDay: It displays the current day visible when the dialog is displayed.

The TimePickerDialog class also consists of a constructor with five arguments listed below:

  1. Context: It requires the application context.
  2. Callback function: onTimeSet() is called when the user sets the time, with the

    following parameters:

    • int hourOfDay: It stores the currently selected hour of the day from the

      dialog.

    • int minute: It stores the currently selected minute from the dialog.
  3. int mHours: It displays the current hour visible when the dialog is displayed.
  4. int mMinute: It displays the current minute visible when the dialog is displayed.
  5. boolean false: If set to false, the time is displayed in 24-hour format; otherwise, not.
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €4.04 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Example Project Structure for Android Date Time Picker Dialog

The project structure for the Date Time Picker Dialog project consists of two main

components: activity_main.xml and MainActivity.java.

Android Date Time Picker Dialog Project Code

The activity_main.xml contains two buttons to invoke the Date and Time Picker dialogs and

to set the time selected by the user in the two EditText views. The MainActivity.java class

implements the logic for displaying the picker dialogs and setting the selected values in the

respective EditText views.

This tutorial demonstrates effective methods for utilizing Date and Time Picker dialogs in

Android applications. You can download the final Android DateTimePickerDialog project via

the provided link and experiment with it.

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technical editorial team

The centron editorial team writes guides from day-to-day operations: tested on our own platform, run in our data centre in Hallstadt near Bamberg.

Category JavaScript
Share
Still have questions?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

Was this tutorial helpful?

Your answer is stored anonymously and helps us improve our tutorials.

Comments

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.