Android Custom DatePicker- After Select Date close without click OK Button

Please follow and like us:

android custom date picker dialog example tutorial 2017

Hello friends, here is the another one most demanding solution for you. If you want date picker dialog without OK and CANCEL button and choose the date by just click on any date. Let’s see Date Picker Dialog with this customization step by step.

Android Custom Date Picker Dialog Example Explained. After Selecting date the date picker should close dialog & set in android without clicking okay button.

Let’s start Android Custom Date Picker Dialog Example Step by Step.

Step 1: Create view of Date Picker in  layout file

<?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:id="@+id/datePicker1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />
</LinearLayout>

Step 2: Create button and TextView for choose and set Date from DatePicker Dialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@color/colorPrimary"
        android:text="Change Date"

        android:textColor="#fff" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Current Date:"
        android:textColor="#525252"
        android:textSize="18sp" />

</LinearLayout>

Step 3: Main Logic of after selecting the date the date picker should close dialog & set in android without clicking okay button.

package com.nkdroid.datepicker;

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    Button displayDate;
    TextView textview1;
    private int selectedDate,selectedMonth,selectedYear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview1=(TextView)findViewById(R.id.textView1);
        displayDate=(Button)findViewById(R.id.button1);

        displayDate.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                // custom dialog
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.datepickerview);
                dialog.setTitle("");

                DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.datePicker1);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                selectedDate=calendar.get(Calendar.DAY_OF_MONTH);
                selectedMonth=calendar.get(Calendar.MONTH);
                selectedYear=calendar.get(Calendar.YEAR);
                datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        Log.e("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);

                        Log.e("selected date", selectedDate+"");
                        Log.e("selected date", dayOfMonth+"");
                        Log.e("selected month", selectedMonth+"");
                        Log.e("selected month", month+"");

                        Log.e("selected year", selectedYear+"");
                        Log.e("selected year", year+"");
                        if(selectedDate ==dayOfMonth && selectedMonth==month && selectedYear==year) {

                            textview1.setText("Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
                            dialog.dismiss();
                        }else {

                            if(selectedDate !=dayOfMonth){
                                textview1.setText("Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
                                dialog.dismiss();
                            }else {
                                if(selectedMonth !=month){
                                        textview1.setText("Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
                                        dialog.dismiss();
                                }
                            }
                        }
                        selectedDate=dayOfMonth;
                        selectedMonth=(month);
                        selectedYear=year;
                    }
                });


                dialog.show();
            }

        });
    }

}

Hope you liked our modification in Android Custom Date Picker Dialog Example. Stay connected for upcoming new Android tutorials.

nirav kalola