I have develop an easy way out. It is one time investment work
Copy the following class into your project...
public class DatePickerHelper {
/********************************************DatePickerRelatedWork*****************************/
public static void showDatePicker(Activity activity, OnDateSelectedListener listener, boolean shouldUsePreviousDate) {
Calendar currentDate = Calendar.getInstance();
int currentYear = currentDate.get(Calendar.YEAR);
int currentMonth = currentDate.get(Calendar.MONTH);
int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(activity, new MyDateSetListener(listener), currentYear, currentMonth, currentDay);
datePicker.setTitle("Select date");
if (shouldUsePreviousDate){
datePicker.getDatePicker().setMinDate(-1893475799000L);
}else {
datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}
datePicker.show();
}
public static class MyDateSetListener implements DatePickerDialog.OnDateSetListener {
private OnDateSelectedListener listener;
public MyDateSetListener(OnDateSelectedListener listener) {
this.listener = listener;
}
@Override
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
String selectedDateString = null;
selectedMonth = selectedMonth + 1;
if (selectedDay >= 0 && selectedDay < 10 && selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = "0" + selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} else if (selectedDay >= 0 && selectedDay < 10) {
selectedDateString = "0" + selectedDay + "-" + selectedMonth + "-" + selectedYear;
} else if (selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} else {
selectedDateString = selectedDay + "-" + selectedMonth + "-" + selectedYear;
}
listener.onDateSelected(selectedDateString);
}
}
public interface OnDateSelectedListener {
void onDateSelected(String selectedDateString);
}
}
Then copy the below method into your fragment...
private void showDatePickerDialog(){
DatePickerHelper.showDatePicker(AddCashbookActivity.this, new DateTimePickerWork.OnDateSelectedListener() {
@Override
public void onDateSelected(String selectedDateString) {
editText.setText(selectedDateString);
}
},true);
}
Then call the above method from your EditText listener (both
OnFocusChangeListener
and
OnClickListener
)
Below is an example...
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
showDatePickerDialog();
}
}
});
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
Change
editText
variable to your EditText variable name.
And you are done!
Understanding the code
The above class can be use multiple times in your project and you do not need to think about the inner work of the Helper class. The hard part has been done by the class and you only get a callback whenever a date is selected.
The
showDatePicker
method need 3 params.
- Activity object
- An OnDateSelectedListener object - like any other listener
- shouldUsePreviousDate - boolean param, which is responsible for if the date picker should allow user to select past dates or disabled it!
If you need to change the Date format, you may change the method
onDateSet
You may change 10-05-2018 to 10/05/2018 or anything else. Just read the body of the method and you will understand what to change.
Hope this will help you and many others.
EDIT
Add android:focusable="false" within the xml file of the EditText to allow for a single touch and then you can avoid using setOnFocusChangeListener in your java code.