|
| 1 | +package com.amit.views; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.DatePickerDialog; |
| 5 | +import android.app.Dialog; |
| 6 | +import android.app.DialogFragment; |
| 7 | +import android.content.Context; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.support.annotation.NonNull; |
| 10 | +import android.util.Log; |
| 11 | +import android.widget.DatePicker; |
| 12 | + |
| 13 | +import java.text.SimpleDateFormat; |
| 14 | +import java.util.Calendar; |
| 15 | +import java.util.Locale; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by AMIT JANGID on 24-Oct-18. |
| 19 | + * |
| 20 | + * this class will display a date picker dialog |
| 21 | + * it will return selected date in the format defined |
| 22 | +**/ |
| 23 | +@SuppressWarnings("unused") |
| 24 | +public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener |
| 25 | +{ |
| 26 | + private static final String TAG = DatePickerFragment.class.getSimpleName(); |
| 27 | + |
| 28 | + private Calendar calendar; |
| 29 | + private SelectedDate mSelectedDate; |
| 30 | + |
| 31 | + private static boolean mIsCurrentDateMin; |
| 32 | + private static String mSelectedDateFormat; |
| 33 | + |
| 34 | + public void showDatePickerDialog(@NonNull Context context, @NonNull SelectedDate selectedDate, |
| 35 | + @NonNull String selectedDateFormat, boolean isCurrentDateMin) |
| 36 | + { |
| 37 | + mIsCurrentDateMin = isCurrentDateMin; |
| 38 | + mSelectedDateFormat = selectedDateFormat; |
| 39 | + |
| 40 | + DatePickerFragment datePickerFragment = new DatePickerFragment(); |
| 41 | + datePickerFragment.initializeInterface(selectedDate); |
| 42 | + datePickerFragment.show(((Activity) context).getFragmentManager(), "DatePicker"); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Dialog onCreateDialog(Bundle savedInstanceState) |
| 47 | + { |
| 48 | + // Use the current date as the default date in the picker |
| 49 | + calendar = Calendar.getInstance(); |
| 50 | + |
| 51 | + int year = calendar.get(Calendar.YEAR); |
| 52 | + int month = calendar.get(Calendar.MONTH); |
| 53 | + int day = calendar.get(Calendar.DAY_OF_MONTH); |
| 54 | + |
| 55 | + // Create a new instance of DatePickerDialog and return it |
| 56 | + DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day); |
| 57 | + |
| 58 | + if (mIsCurrentDateMin) |
| 59 | + { |
| 60 | + datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); |
| 61 | + } |
| 62 | + |
| 63 | + return datePickerDialog; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) |
| 68 | + { |
| 69 | + calendar.set(year, month, dayOfMonth); |
| 70 | + SimpleDateFormat sdf = new SimpleDateFormat(mSelectedDateFormat, Locale.getDefault()); |
| 71 | + |
| 72 | + String selectedDate = sdf.format(calendar.getTime()); |
| 73 | + Log.e(TAG, "onDateSet: selected date is: " + selectedDate); |
| 74 | + |
| 75 | + if (mSelectedDate != null) |
| 76 | + { |
| 77 | + mSelectedDate.selectedDate(selectedDate); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void initializeInterface(SelectedDate selectedDate) |
| 82 | + { |
| 83 | + this.mSelectedDate = selectedDate; |
| 84 | + } |
| 85 | + |
| 86 | + public interface SelectedDate |
| 87 | + { |
| 88 | + void selectedDate(String selectedDate); |
| 89 | + } |
| 90 | + |
| 91 | + private class InvalidDateFormatException extends RuntimeException |
| 92 | + { |
| 93 | + String message; |
| 94 | + |
| 95 | + private InvalidDateFormatException(String message) |
| 96 | + { |
| 97 | + super(message); |
| 98 | + this.message = message; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments