How To Popup Datepicker When Click On Edittext In Fragment
Solution 1:
Answer :
Make your EditText clickable and focusable false
like,
<EditText
android:id="@+id/etDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:hint="Select" />
Then in your Fragment Class, put below code,
@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Viewview= inflater.inflate(R.layout.layout_xxx, container, false);
EditText etDOB=view.findViewById(R.id.etDOB);
etDOB.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
openDatePickerDialog(v);
}
});
}
publicvoidopenDatePickerDialog(final View v) {
// Get Current DateDatePickerDialogdatePickerDialog=newDatePickerDialog(getActivity(),
(view, year, monthOfYear, dayOfMonth) -> {
StringselectedDate= dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
switch (v.getId()) {
case R.id.etDOB:
((EditText)v).setText(selectedDate);
break;
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
datePickerDialog.show();
}
Solution 2:
Try this in the XML file:
<EditText
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="true"
android:editable="false"
android:hint="@string/birthday"/>
Now in Java File:
Calendar myCalendar = Calendar.getInstance();
EditText edittext= (EditText) findViewById(R.id.Birthday);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
publicvoidonDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
edittext.setOnClickListener(new OnClickListener() {
@Override
publicvoidonClick(View v) {
// TODO Auto-generated method stubnew DatePickerDialog(classname.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
Now add the method in the above activity.
privatevoidupdateLabel() {
StringmyFormat="MM/dd/yy"; //In which you need put hereSimpleDateFormatsdf=newSimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}
Add android:focusable="false"
within the xml file of the EditText to allow for a single touch.
Solution 3:
Step 1. Disable your edit text
'enabled=false'
Step 2. Set onClickListener on your Edit Text
Step 3. onClick of your EditText, call the function to open the Date picker Dialogue.
and onDateSelected, the overridden function will return date, you can extract the date, month and year from there and set that text as string in your Edit text.
Let me know if you need help with the code.
Solution 4:
Edittext edittext= findViewById(R.id.edittext);
private void getDate() {
edtitext.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
CalendarmcurrentDate= Calendar.getInstance();
mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
mMonth = mcurrentDate.get(Calendar.MONTH);
mYear = mcurrentDate.get(Calendar.YEAR);
DatePickerDialogdatePickerDialog=newDatePickerDialog(AddDetails.this, newDatePickerDialog.OnDateSetListener() {
@OverridepublicvoidonDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
date = dayOfMonth + ":" + month + ":" + year;
edittext.setText(date);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
}
Solution 5:
I have develop an easy way out. It is one time investment work
Copy the following class into your project...
publicclassDatePickerHelper {
/********************************************DatePickerRelatedWork*****************************/publicstaticvoidshowDatePicker(Activity activity, OnDateSelectedListener listener, boolean shouldUsePreviousDate) {
CalendarcurrentDate= Calendar.getInstance();
intcurrentYear= currentDate.get(Calendar.YEAR);
intcurrentMonth= currentDate.get(Calendar.MONTH);
intcurrentDay= currentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialogdatePicker=newDatePickerDialog(activity, newMyDateSetListener(listener), currentYear, currentMonth, currentDay);
datePicker.setTitle("Select date");
if (shouldUsePreviousDate){
datePicker.getDatePicker().setMinDate(-1893475799000L);
}else {
datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}
datePicker.show();
}
publicstaticclassMyDateSetListenerimplementsDatePickerDialog.OnDateSetListener {
private OnDateSelectedListener listener;
publicMyDateSetListener(OnDateSelectedListener listener) {
this.listener = listener;
}
@OverridepublicvoidonDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
StringselectedDateString=null;
selectedMonth = selectedMonth + 1;
if (selectedDay >= 0 && selectedDay < 10 && selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = "0" + selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} elseif (selectedDay >= 0 && selectedDay < 10) {
selectedDateString = "0" + selectedDay + "-" + selectedMonth + "-" + selectedYear;
} elseif (selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} else {
selectedDateString = selectedDay + "-" + selectedMonth + "-" + selectedYear;
}
listener.onDateSelected(selectedDateString);
}
}
publicinterfaceOnDateSelectedListener {
voidonDateSelected(String selectedDateString);
}
}
Then copy the below method into your fragment...
privatevoidshowDatePickerDialog(){
DatePickerHelper.showDatePicker(AddCashbookActivity.this, newDateTimePickerWork.OnDateSelectedListener() {
@OverridepublicvoidonDateSelected(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(newView.OnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View view, boolean hasFocus) {
if (hasFocus){
showDatePickerDialog();
}
}
});
editText.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(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.
Post a Comment for "How To Popup Datepicker When Click On Edittext In Fragment"