Android DatePicker


Android DatePicker Tutorials and Examples.

A DatePicker is a widget that allows us select a date.

It's existed API Level 1 and derives from FrameLayout.

You can use the DatePicker to select year, month and day. You can use a Spinner or CalenderView as long as you set the R.styleable.DatePicker_datePickerMode attribute.

The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed.

When the R.styleable.DatePicker_datePickerMode attribute is set to calendar, the month and day can be selected using a calendar-style view while the year can be selected separately using a list.

DatePickerDialog is a dialog that makes use of DatePicker.

DatePicker API Definition

DatePicker has been around since the beginning of android, API Level 1.

It's a class deriving from the FrameLayout.

public class DatePicker extends FrameLayout 

Let's see datepicker's inheritance hierarchy:

java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout
               ↳    android.widget.DatePicker

Quick DatePicker Examples

Android DatePicker Examples

Example 1
(a). MainActivity.java

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java . useful . Calendar ;

public class MainActivity extends AppCompatActivity {      

    private TimePicker timePicker ;   
    private DatePicker datePicker;
    Private Calendar calendar ;  
    private int year;
    private int month;
    private int day ;  
    private int hour;
    private int minute;
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main); 
        setContentView(R.layout.main);
        / / Get an object of the calendar 
        calendar = Calendar.getInstance();
        / / Get the year, month, day, minute, second 
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR);
        minute = calendar.get(Calendar.MINUTE);
        setTitle(year+"-"+ month%12 +"-"+day+"-"+" "+hour+":"+minute);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);

        datePicker.init(
                year, month, day, new DatePicker.OnDateChangedListener() {
                    @Override 
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        setTitle(year+"-"+monthOfYear%12+"-"+dayOfMonth);
                    } 
                }); 
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override 
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);
            } 
        }); 

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override 
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-"+(monthOfYear+1)+"-"+dayOfMonth);
            } 
        },year,calendar.get(Calendar.MONTH),day).show();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override 
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay+":"+minute);
            } 
        },hour,minute,true).show();
    } 

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

    <DatePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker" />

    <TimePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker"
        android:layout_gravity="bottom" />

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post