Tutorials  /  JavaScript

Optimal Utilization of Date and Time Pickers in Android Applications

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

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 €3.12 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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren