Open a DatePickerDialog on Click of EditText takes two clicks
public class MainActivity extends Activity {
private Calendar cal;
private int day;
private int month;
private int year;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et= (EditText) findViewById(R.id.edittext1);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
et.setText(day+"/"+month+"/"+"/"+year);
et.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DateDialog();
}
});
}
public void DateDialog(){
OnDateSetListener listener=new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
et.setText(dayOfMonth+"/"+monthOfYear+"/"+year);
}};
DatePickerDialog dpDialog=new DatePickerDialog(this, listener, year, month, day);
dpDialog.show();
}
}
Add this in your edittext to open date picker at first click.
android:focusable="false"
I'll try to address your problem, but I am not completely sure about the first reason.
The calendar opening only on the second click is because you are using an edittext. On the first click, your Edit Text will get focus. then the second click only calls the onClickListener.
If you are not looking forward to edit the date set manually (using keyboard), then why not using a TextView to display the selected Date?
The problem with the date not updating in editText is occurring because you are not setting the DateSetListener in your code. You need to set that to notify the system that a Date was set. The DateChange listener only returns the date while you are changing the date, and it doesn't appear that you are setting the date in the EditText.
Try this code:
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
DatePickerDialog datePicker = new DatePickerDialog(this,
R.style.AppBlackTheme,
datePickerListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
datePicker.setCancelable(false);
datePicker.setTitle("Select the date");
return datePicker;
}
} catch (Exception e) {
showMsgDialog("Exception",
"An error occured while showing Date Picker\n\n"
+ " Error Details:\n" + e.toString(), "OK");
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
String year1 = String.valueOf(selectedYear);
String month1 = String.valueOf(selectedMonth + 1);
String day1 = String.valueOf(selectedDay);
TextView tvDt = (TextView) findViewById(R.id.tvDate);
tvDt.setText(day1 + "/" + month1 + "/" + year1);
}
};
In this, I am updating the date to a TextView with the ID "tvDate". I advise using a TextView instead of EditText and try this code.
Update
If you need to use EditText and load the calender in the first click, then try setting an onFocusListner to the editText instead of onClickListner.
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// Show your calender here
} else {
// Hide your calender here
}
}
});