I'm trying to show a DatePickerDialog once I click a button, but once I click it, it just stop the app and throw the following Exception:
05-06 08:46:00.688 18786-18786/carloscoronado.chillout E/AndroidRuntime: FATAL EXCEPTION: main
Process: carloscoronado.chillout, PID: 18786
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:694)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at carloscoronado.chillout.reservar_restaurant$3.onClick(reservar_restaurant.java:188)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
This is the code of the Activity:
public class reservar_restaurant extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
int mDay = 0;
int mMonth = 0;
int mYear = 0;
int mHour = 0;
int mMinute =0;
Button buttonDate;
Calendar calender = Calendar.getInstance();
DatePickerDialog theDatePickerDialog;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservar_restaurant);
buttonDate = (Button) findViewById(R.id.btn_fecha);
Calendar c = Calendar.getInstance();
mDay = c.get(Calendar.DAY_OF_MONTH);
mMonth = c.get(Calendar.MONTH);
mYear = c.get(Calendar.YEAR);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
buttonDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//onCreateDialog(DATE_DIALOG_ID);
long actualDay= calender.getTimeInMillis();
final long oneDay = 24 * 60 * 60 * 1000L;
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
datePickerDialog.getDatePicker().setMinDate(actualDay);
datePickerDialog.getDatePicker().setMaxDate(actualDay+(oneDay*7));
datePickerDialog.setTitle("Pick your reservation day");
datePickerDialog.show();
}
});
}
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mYear = year;
mMonth = month + 1;
mDay = dayOfMonth;
String sYear = String.valueOf(mYear);
String sMonth = String.valueOf(mMonth);
String sDayOfMonth = String.valueOf(mDay);
if(sMonth.length() <= 1){
sMonth = "0" + sMonth;
}
if (sDayOfMonth.length() <= 1){
sDayOfMonth = "0" + sDayOfMonth;
}
text7.setText(sDayOfMonth + "/" + sMonth + "/" + sYear );
}
}
The Exception throws me to the following line of code:
datePickerDialog.show();
what could it be??
Instead of passing getApplicationContext() just pass activity context.
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
You need to pass Context that is an Activity when you are displaying dialog in your app.
Instead of:
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
use: Activity context..
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
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 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();
}
I want to set restrictions on the Datepicker so that it will allow the user to only choose the date 7 days from now. I also want to set restrictions on the Timepicker so that the user can only choose the time between 11:00 am and 6:00 pm. Here is my Code for the DatePicker and TimePicker.
private void showTimeDialog(final EditText timeInput) {
final Calendar calendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
timeInput.setText(enteredTime.format(calendar.getTime()));
time = enteredTime.format(calendar.getTime());
}
};
new TimePickerDialog(OrderPickupActivity.this, timeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show();
}
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
new DatePickerDialog(OrderPickupActivity.this, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
Can you please help me out?
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(OrderPickupActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 7); // Adding 7 days
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.show();
}
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();
}
TimePicker shows current time as default in TimePicker, but what if i want to set default time in time picker as per my requirement.
Like its 11:10 when i am writing this, but in TimePicker i like to show 01:00 as default (my mean 2 hours difference, and minutes should be 00 only)
static final int TIME_DIALOG_ID = 1;
public int year,month,day,hour,minute;
private int mYear, mMonth, mDay,mHour,mMinute;
public TimePickerActivity() {
// Assign current Date and Time Values to Variables
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
String formattedMinutes = "" + min;
String formattedHour = "" + hourOfDay;
if (hourOfDay < 10) {
formattedHour = "0" + hourOfDay;
}
if (min < 10) {
formattedMinutes = "0" + min;
}
textTime.setText(formattedHour + ":" + formattedMinutes);
}
};
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
TimePickerDialog timePickerDialog = new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
return timePickerDialog;
}
return null;
}
You can do this with following code:
private TimePicker timePicker;
timePicker = (TimePicker) dialog.findViewById(R.id.timePickerDialog);
if(DateFormat.is24HourFormat(getActivity()){
timePicker.setIs24HourView(true);
}else {
timePicker.setIs24HourView(false);
}
// here you can define your hour and minute value.
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
You can try below code
SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);
TimePicker picker = new TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));