DatePicker in Android

DatePicker in Android

DatePicker in Android

In this Blog Post, we can study “What is DatePicker in Android”.

DatePicker is used to select a date.

Android Date picker allows you to select dates consisting of day, month, and year in your custom user interface.

For this android provides DatePicker and DatePickerDialog components.

In order to show DatePickerDialog, you have to pass the DatePickerDialog id to the showDialog(id_of_dialog) method.

android.widget.DatePicker is the subclass of FrameLayout.


following are some of the methods of DatePicker:-

Sr.No Method & description
1 getDayOfMonth()

It gets the selected day of the month

2 getMonth()

It gets the selected month

3 getYear()

It gets the selected year

4 setMaxDate(long maxDate)

It sets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone

5 setMinDate(long minDate)

It sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone

6 setSpinnersShown(boolean shown)

It sets whether the spinners are shown

7 updateDate(int year, int month, int dayOfMonth)

It updates the current date

8 getCalendarView()

It will returns the calendar view

9 getFirstDayOfWeek()

It will returns the first day of the week

 

DatePickerDialog class consists of a 5 argument constructor with parameters listed below.

1) Context: It requires the application context.

2) CallBack Function: onDateSet() is invoked when the user sets the date with the following parameters:

  1. int year: stores current selected year.
  2. // int datatype- int monthOfYear: store currently selected month.
  3. int dayOfMonth: store current selected day from the dialog.

3) int mYear:  shows the current year.

4)int mMonth: shows the current month.

5)int mDay: shows the current day.

activity_main.xml file:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.datepicker.MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:text="" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:text="Set Date" />

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="30dp" />

</RelativeLayout>

MainActivity.java file:-

package com.example.datepicker;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    DatePicker picker;
    Button displayDate;
    TextView textview1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview1=(TextView)findViewById(R.id.textView1);
        picker=(DatePicker)findViewById(R.id.datePicker);
        displayDate=(Button)findViewById(R.id.b1);

        textview1.setText("Current Date: "+getCurrentDate());

        displayDate.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                textview1.setText("Change Date: "+getCurrentDate());
            }

        });

    }
    public String getCurrentDate(){
        StringBuilder builder=new StringBuilder();;
        builder.append((picker.getMonth() + 1)+"/");//month is 0 based
        builder.append(picker.getDayOfMonth()+"/");
        builder.append(picker.getYear());
        return builder.toString();
    }
}

Output:

DatePicker in Android

 

Also Read This:

Activity Lifecycle in Android

Intent in Android

 

Harsh

Hey Friends, Welcome to thetechbuddy. This blog is about technical or programming knowledge.

Leave a Reply