Create Show Material Design DatePicker Dialog for android kitkat 4.0 pre lollipop devices

How to make Material style Date Picker Dialog inside old android version devices using support GitHub libraries.

In this tutorial we are going to create fully material style date picker dialog inside android application. This tutorial is mostly helpful for all the android developers who wish to use Show Material Design DatePicker Dialog into old android version devices like Kitkat, Jelly bean, Ice Create Sandwich etc. So here is the complete step by step tutorial.

android-project-download-code-button

Note :  Please follow the below steps to add support library in your android application project :

1. Open your project’s build.gradle ( Module : app ) file.

build-gradle-app

2. Please add below code inside your build.gradle ( Module : app ) file.

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.wdullaer:materialdatetimepicker:2.3.0'

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

time_picker_library

Here your go friends….Now We are going start coding.
Code for MainActivity.java file.

package com.android_examples.materialdesigndatepickerdialog_android_examplescom;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import android.widget.Button;
import android.widget.Toast;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

    Button button;
    Calendar calendar ;
    DatePickerDialog datePickerDialog ;
    int Year, Month, Day ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        calendar = Calendar.getInstance();

        Year = calendar.get(Calendar.YEAR) ;
        Month = calendar.get(Calendar.MONTH);
        Day = calendar.get(Calendar.DAY_OF_MONTH);



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

                datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);

                datePickerDialog.setThemeDark(false);

                datePickerDialog.showYearPickerFirst(false);

                datePickerDialog.setAccentColor(Color.parseColor("#009688"));

                datePickerDialog.setTitle("Select Date From DatePickerDialog");

                datePickerDialog.show(getFragmentManager(), "DatePickerDialog");

            }
        });
    }

    @Override
    public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {

        String date = "Selected Date : " + Day + "-" + Month + "-" + Year;

        Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.materialdesigndatepickerdialog_android_examplescom.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK HERE TO show material design datepicker dialog "
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Screenshots:

DatePickerDialog-1

Create Show Material Design DatePicker Dialog for android kitkat 4.0 pre lollipop devices

DatePicker Dialog-3

Click here to download Create Show Material Design DatePicker Dialog for android kitkat 4.0 pre lollipop devices project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *