DatePicker in Android

  • Post category:Android / Blog

DatePicker Dialog is normally used in most application to select date. By the help of this, we can pick date from DatePicker dialog. In this tutorial we will see how to implement DatePicker in android. Let’s start to build now…

Step 1 Layout File for DatePicker in Android(activity_date_picker.xml)

<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=".DatePickerActivity">

    <LinearLayout
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/tvDate"
            android:layout_marginBottom="20dp"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textAlignment="center"/>

        <Button
            android:id="@+id/btnPickDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Pick Date"/>

    </LinearLayout>

</RelativeLayout>

Step 2 DatePickerActivity.java (Java File Code is Here)

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class DatePickerActivity extends AppCompatActivity {
    Button btnPickDate;
    TextView tvDate;

    int year, month, day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);

        tvDate = findViewById(R.id.tvDate);
        btnPickDate = findViewById(R.id.btnPickDate);

        btnPickDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // the instance of our calendar.
                final Calendar c = Calendar.getInstance();
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);

                picDate();

            }
        });
    }

    private void picDate() {
        DatePickerDialog datePickerDialog = new DatePickerDialog(DatePickerActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        tvDate.setText(dayOfMonth+"-"+(month+1)+"-"+year);
                    }
                },year,month,day);

        datePickerDialog.show();
    }

}