How to create Date Picker with Fragment Dialog in Android

In this android programming source code example, we are going to create Date Picker with Fragment Dialog in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create Date Picker with Fragment Dialog in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_centerInParent="true"
        android:onClick="ShowDatePicker"
        android:text="ShowDatePicker"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;

public class DatePickerAndTimePickerActivity6 extends AppCompatActivity implements ChooseDateFragment.DateListner{
    private int year, month, day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_and_time_picker6);

    }

    public void ShowDatePicker(View view){
       ChooseDateFragment chooseDateFragment = new ChooseDateFragment();
       chooseDateFragment.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onDateSelected(int year, int month, int day){
        this.day = day;
        this.month = month;
        this.year = year;
    }
}

fragment_choose_date.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <DatePicker
        android:id="@+id/datepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:calendarViewShown="false"/>


    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Accept"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_below="@id/datepicker"/>

</RelativeLayout>

ChooseDateFragment.java

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;


public class ChooseDateFragment extends DialogFragment implements View.OnClickListener {

    private DatePicker datePicker;
    private Button button;
    private boolean isDatesetted = false;
    private int year;
    private int month;
    private int day;
    private DateListner listner;

    public interface DateListner{
        public void onDateSelected(int year, int month, int day);
    }

    public ChooseDateFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_choose_date, container, false);
        getDialog().setTitle("DatePicker");
         datePicker = (DatePicker) rootview.findViewById(R.id.datepicker);
        button = (Button) rootview.findViewById(R.id.button);
        button.setOnClickListener(this);
        if(isDatesetted){
            datePicker.updateDate(year, month, day);
        }

        return rootview;
    }

    @Override
    public void onClick(View view) {
       switch (view.getId()){
           case R.id.button:
               int year = datePicker.getYear();
               int month = datePicker.getMonth()+1;
               int day = datePicker.getDayOfMonth();
               listner.onDateSelected(year, month, day);
               break;
       }
       this.dismiss();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listner = (DateListner) context;
    }

    public void setDate(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
        this.isDatesetted = true;
    }


}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.