I have already read this post but nothing helped me. The listener is still not called.
Here is my code:
private void callDatePicker() {
// получаем текущую дату
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
dl = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String editTextDateParam = dayOfMonth + "." + (monthOfYear + 1) + "." + year;
date_text.setText(editTextDateParam);
Log.d("BIMI", editTextDateParam);
}
};
// инициализируем диалог выбора даты текущими значениями
DatePickerDialog datePickerDialog = new DatePickerDialog(this, dl, mYear, mMonth, mDay);
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
String editTextDateParam = mDay + "." + (mMonth + 1) + "." + mYear;
Log.d("GROM", editTextDateParam);
date_text.setText(editTextDateParam);
}
}
});
datePickerDialog.show();
}
callDatePicker is called in the AlertDialog when the button is clicked.
If you set both BUTTON_POSITIVE click listener and OnDateSetListener then only the positive button click will be called. If you want to call OnDateSetListener then don't set the button. If you need to change properties of a button like color,text then do it on setOnShowListener. So your final code will look something like this.
private void callDatePicker() {
// получаем текущую дату
int mYear,mMonth,mDay;
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener dl = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String editTextDateParam = dayOfMonth + "." + (monthOfYear + 1) + "." + year;
// date_text.setText(editTextDateParam);
Log.d("BIMI", editTextDateParam);
}
};
// инициализируем диалог выбора даты текущими значениями
DatePickerDialog datePickerDialog = new DatePickerDialog(this, dl, mYear, mMonth, mDay);
datePickerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button pButton = datePickerDialog.getButton (DialogInterface.BUTTON_POSITIVE);
pButton.setText ("Set date");
}
});
datePickerDialog.show();
}
Related
I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.
I set the Data which user selected in Calander startDate and set this data as MinDate at end date.
I've tried in this way but it's not working :(
I can't use Mateiral Range Date Picker because I have to use specific theme.
Also I'm not good at programming so I'd appreciate it if you could explain it in detail.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
final Calendar c = Calendar.getInstance();
startYear = c.get(Calendar.YEAR);
startMonth = c.get(Calendar.MONTH);
startDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month+1;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, startYear, startMonth, startDay);
dpd.show();
}
});
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
endYear = c.get(Calendar.YEAR);
endMonth = c.get(Calendar.MONTH);
endDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endYear = year;
endMonth = month;
endDay = dayOfMonth;
ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
}
}, endYear,endMonth, endDay);
dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
dpd.show();
}
});
This code:
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
Should be inside
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()
So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog
I have a DatePickerDialog, which is showing everything correct and also selecting the correct value. But I want to set the Month name Ex. "Jan" instead of M01.
Please see the below Image:
https://drive.google.com/file/d/1mhWPCPkARY4I1dvYZ6RdrXueUhHvMa4b/view
See the code:
public void setDueDate() {
if (mYear == 0) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, final int year,
final int monthOfYear, final int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String strDate = mYear + "-" + (mMonth + 1) + "-" + mDay;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(strDate);
tvDueDate.setText(format.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
datePickerDialog.setTitle("");
datePickerDialog.show();
}
I have two date picker From and To, here i can set From datepicker value as minimum date of to datepicker.
Now my requirement is ,if i select To datepicker date, i want to set it as maximum date of From datepicker date. I tried one solution, but its not working in my case.
private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;
Fromdatepicker OnDateSetListener method
fromDateCalendar = Calendar.getInstance();
fromDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
fromDateCalendar.set(Calendar.YEAR, year);
fromDateCalendar.set(Calendar.MONTH, monthOfYear);
fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" +
(fromDateCalendar.get(Calendar.MONTH)+1) + "-" +
fromDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewFrom.setText(fromDate);
toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}
};
To datepicker OnDateSetListener method
toDateCalendar = Calendar.getInstance();
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewTo.setText(toDate);
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
}
};
toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener,
toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH),
toDateCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener,
fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH),
fromDateCalendar.get(Calendar.DAY_OF_MONTH));
Textview listner for call datepicker dialog
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fromDatePicker.show();
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toDatePicker.show();
}
});
I tried
fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
in onDateSet method of toDatePickerListener, but its not
working
I have tried the following way,Hope it may help you.
private String selectedToDate = "";
private String selectedFromDate = "";
private Calendar calToDate, calFromDate;
Write this code in onCreate() method
calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);
Todate and FromDate TextView Clicks
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showFromDatePicker(fromdateListener, calFromDate, calToDate);
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showToDatePicker(todateListener, calToDate, calFromDate);
}
});
toDate and fromDate Listener
public DatePickerDialog.OnDateSetListener fromdateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calFromDate.set(year, monthOfYear, dayOfMonth);
selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
textviewFrom.setText(selectedFromDate);
}
};
DatePickerDialog.OnDateSetListener todateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calToDate.set(year, monthOfYear, dayOfMonth);
selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
textviewTo.setText(selectedToDate);
}
};
private String returnFormattedDate(long timeInMilliSec) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(new Date(timeInMilliSec));
}
Methods to show DatePicker
private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar minDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (minDateCalender != null) {
dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
}
dialog.show();
}
private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar maxDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());
dialog.show();
}
Method to convert string date to Calender
private Calendar convertToCalender(String date) {
Calendar mCalender = Calendar.getInstance();
if (date == null) {
return mCalender;
} else {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date returnDate = formatter.parse(date);
mCalender = Calendar.getInstance();
mCalender.setTime(returnDate);
return mCalender;
} catch (ParseException e) {
e.printStackTrace();
return mCalender;
}
}
}
If you are are using DatePicker widget in xml the following will work .
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
fromdatePicker.setMaxDate(toDateCalendar.getTimeInMillis());
//etToDate.setText(toDate);
}
};
Or if you opening DatePickerDialog oen after another . Then you should save the previously selected date and pass it to next DatePickerDialog before calling show.
pickerDialog.getDatePicker().setMaxDate(maxTime);// Set Max and min here
pickerDialog.show();
I am not able to understand where am I doing mistake. I want to check it is a weekend or weekday. If it is weekend then it should prompt in the toast that it is a sunday or saturday weekend. I don't know what should I put in place of ??? mark. It should also display current date if it is weekend. Please, any one can help me. Thank you.
View.OnClickListener//,
// TimePickerDialog.OnTimeSetListener
// TimePickerDialog.OnTimeSetListener
{
//Declaration for class
ButtonViews views;
dpListener dpListenerView;
// Declartion for member vairables
int day, month, x_year;
int hour;
int minute;
Calendar calendar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
views = new ButtonViews();
dpListenerView = new dpListener();
//ButtonListener
views.button_date.setOnClickListener(this);
views.button_time.setOnClickListener(this);
//
// pick up the default date using Calender class
calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
/*
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);
*/
curr_date();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
setupDate(day, month, x_year);
setupTime(hour, minute);
}
public void curr_date(){
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDatePickerDialog();
break;
case R.id.button_time:
break;
}
}
private void setupTime(int hours, int minutes) {
views.button_time.setText(hours + ":" + minutes);
}
private void setupDate(int day, int month, int year) {
String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
}
private void showDatePickerDialog() {
DatePickerDialog datepickerdialog = new DatePickerDialog
(
this,
dpListenerView,
/* new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
} },*/
//this,
x_year,
month,
day
);
datepickerdialog.show();
}
class ButtonViews {
Button button_time;
Button button_date;
public ButtonViews() {
button_date = (Button) findViewById(R.id.button_date);
button_time = (Button) findViewById(R.id.button_time);
}
}
class dpListener implements OnDateSetListener {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
// c.setTime();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == 7 || dayOfWeek == 1) {
// as your requirement: you should display mesage they can not select the weekend" here
// then you set the value in datepickerdialog by current date
Toast.makeText(DateTimePickerActivity.this
, "You have selected weekend"
, Toast.LENGTH_SHORT).show();
}
}
}
}
Once you get your date you can do like this and get the day and proceed with your logic.
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
I think that your variable dayOfWeek is the same variable as the parameter dayOfMonth
The above code must work
public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if (selectedDay == 1 || selectedDay == 7) {
Toast.makeText(DateTimePickerActivity.this
, "You have selected weekend"
, Toast.LENGTH_SHORT).show();
view.updateDate(selectedYear,selectedMonth,selectedDay);
}
}
};
This question already exists:
edittext not updated when set new date
Closed 9 years ago.
this is my code which work fine updare edittext but problem is edittext not update imediately until i click again on edittext tell me how do i update edittext on datepicker dailog (positive button) h date picker display two default button positive and negative how do i update editteext on positive button help me please
EditText edtKeyword;
String selectedDate;
int mYear,mMonth,mDay ;
DateFormat fmtDateAndTime = DateFormat.getDateInstance();
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
edtKeyword = (EditText) findViewById(R.id.edtKeyword);
edtKeyword.setClickable(true);
edtKeyword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MenuList.this, d, myCalendar
.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
mYear = myCalendar.get(Calendar.YEAR);
mMonth = myCalendar.get(Calendar.MONTH);
mDay = myCalendar.get(Calendar.DAY_OF_MONTH);
selectedDate= mMonth+"-"+mDay+"-"+mYear;
edtKeyword.setText(selectedDate);
}
});
edtKeyword.setText(fmtDateAndTime.format(myCalendar.getTime()));
I tried this code. It works fine for me. Please try it once
private int pYear = 1970;
private int pMonth = 1;
private int pDay = 1;
static final int DATE_DIALOG_ID = 0;
Then in onCreate:
edt_birth.setKeyListener(null);
edt_birth.setOnTouchListener(new OnTouchListener() {
#SuppressWarnings("deprecation")
#Override
public boolean onTouch(View v, MotionEvent event) {
showDialog(DATE_DIALOG_ID);
return false;
}
});
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
pDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay() {
int temp = pMonth + 1;
edt_birth.setText(pDay + "/" + temp + "/" + pYear);
}
This will update your edittext.