in my application i have to date picker as From date and To date. I am using onCreateDialog() method. So first time i am selecting the dates from date picker and putting them in the edit text. then when again i am selecting the date from the date picker, in the date picker it is showing the date which i already selected. But if i move to other search in the same screen(Activity) and try to pick the date from date picker, date picker still is showing the previous date. not the current date.
protected Dialog onCreateDialog(int id) {
final Calendar c = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
case DATE_DIALOG_ID2:
return new DatePickerDialog(this, mDateSetListener2, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
}
return null;
}
May be you can try writing your question clearly once again, but to my understanding of your question you might want to look at your onDateChanged() callback method. I assume you have a updateDialog() method inside this onDateChanged(), you might want to check that method for err's.
I suppose you looked this link where they showed how to use a date picker http://developer.android.com/resources/tutorials/views/hello-datepicker.html
good luck
For the 2nd question you asked about mixup in from and to date dialog
From what you are saying, I think there something like a small error like
1) R.id.datepicker2 being used in place of R.id.datepicker1
or
2) having mixs up in onDateChangedListener(). I am sure there is better way to do it, but try checking whether you have like this below 2 different listeners
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePicker fromDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker1 is changed in your case the From date field
}
});
DatePicker toDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker2 is changed in your case the To date field
}
});
Related
One of my features require the user to select a month and year, so the following is the method which will pop up the first attached image (d/m/y).
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//Check if selected month/year is in future of today.
}
}, year, month, day);
dp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dp.setTitle("Set Monthly Overview");
dp.show();
However, i do NOT require the date. How can i disable user from selecting it, or best of all, to even not display the date option there?
What I wish to achieve is something like the following image in the best case scenario.
Refer "Android DatePicker change to only Month and Year" which will help you just display month and year in the datepicker. Hope this helps.
How to show 1980 as initial Year in DatePicker dialog, still i am getting initial date as current date
see my code below, and let me know where i have to do changes in my code
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
return null;
That is because the last 3 parameters on the DatePickerDialog constructor are the date to be shown initially in the DatePicker. You are setting the dateTime variable to the current day with
private Calendar dateTime = Calendar.getInstance();
So you must set your dateTime variable to the correct date you want to be shown on the first run. In your OnCreate() method do this
dateTime.set(1980, Calendar.JANUARY , 1);
Now your DatePicker should show the correct value on the first run and since you set the dateTime variable in the callback it should set the correct previous value if the user opens up the dialog again.
I need a date-picker dialog in my application. First, I open the date picker dialog box and I select the date. The next time whenever open Date pickerDialog, the date of date picker should be current date of device. However, it shows last selected date in dialog. Please help me to set the code. Here is my code as I have it now:
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
Date d=new Date();
Calendar c=Calendar.getInstance();
c.setTimeZone(tz);
c.setTime(d);
d=c.getTime();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
DatePickerDialog datePicker
= new DatePickerDialog(this, datePickerListener, year, month, date);
return datePicker;
}
return super.onCreateDialog(id);
}
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)
{
cors_year = selectedYear;
cors_month = selectedMonth;
cors_date = selectedDay;
getDate("calledfromDialog");
displaySunTime();
displayChoghadiya("Day");
displayTime(3, 1);
dayButtonClicked();
selected_DAY_NIGHT="Day";
}
};
Note that onCreateDialog was deprecated in v13, per the documentation:
Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.
Which you would use along with this tutorial
However, if you want to stay with the DatePickerDialog, you can use code such as:
protected void onPrepareDialog (int id, Dialog dialog)
{
DatePickerDialog datePickerDialog = (DatePickerDialog) dialog;
// Get the current date
datePickerDialog.updateDate(year, month, day);
}
This is because Android only calls onCreateDialog once per dialog to re-use the dialog. onPrepareDialog is called in order for you to set the state of the Dialog appropriately before it is shown.
I have the following widget in my app. Where it says set date is it possible to replace this with the day of week, day of month, month and year. eg wed 07 Nov 2012?
I'm running Android 4 on htc whereas i know if i run the same code on Android 2.3.3 the the date is displayed how i want it.
.[update1]
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this,2 ,datePickerListener, year, month, day);
}
return null;
}
.[update2]
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
final DateFormat df = DateFormat.getDateInstance(0);
final Calendar mCal = Calendar.getInstance();
// Create a date picker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(this,2,datePickerListener, year, month, day){
#Override
public void onDateChanged(DatePicker view, int year,int month, int day){
mCal.set(Calendar.YEAR, year);
mCal.set(Calendar.MONTH, month);
mCal.set(Calendar.DAY_OF_MONTH, day);
setTitle(df.format(mCal.getTime()));
}
};
return datePickerDialog;
//return new DatePickerDialog(this,2 ,datePickerListener, year, month, day);
}
return null;
}
Yes. I did it with following code.. You will have to change according to your requirement.
dPicker.init((previouslyEnteredDate.getYear() + 1900),
previouslyEnteredDate.getMonth(),
previouslyEnteredDate.getDate(), null);
If you just want to initialize it with the current date as a reminder of what today is (or at least don't need it to change as the user interacts with the picker) you can do it by creating a temporary variable and calling the setTitle(CharSequence) method on it (since DatePickerDialog extends AlertDialog)
for example:
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog dpd = new DatePickerDialog(this, 2, datePickerListener,
year, month, day);
dpd.setTitle("Today is: " + year + "-" + (month + 1) + "-" + day);
return dpd;
Okay, I got a bit lazy with the formatting, there, obviously, but I hope you get the basic idea.
If you want the title to update as they are changing the date on the picker, that gets more involved, but I can't imagine that's what you're going for (unless the point is to see the day of the week in context as they are changing it?). If it is, let me know and I'll elaborate.
Hi every one thanks in advance..
I have requirement in my App that I have a Date Picker to set the date. Now I want to restrict the Date Picker to set the date above System current dates but not below to the System current date. How....?
Can any one help me out from this.... And How to validate the System current date with the Date Picker Date.
In this case you can extend DatePickerDialog and make your own implementation of OnDateChanged, which is called everytime that the date changes and you get as parameters the DatePicker, and the new year, month and day values, so you can check if that date is past and in that case throw the error (with a Toast or whatever) and call DatePicker.updateDate() to set a correct value (so that DatePicker is allways in a consistent state).
Also, you can call to DatePicker.init(year, monthOfYear, dayOfMonth, onDateChangedListener); then you can pass a
onDateChangedListener implementation without having to extend DatePickerDialog.
EDIT: (I never try this but I think it can done your work..)
DatePicker's
setMinDate(long minDate)
Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Example:
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMinDate(new Date());
Use setMinDate function of CalendarView class. Here you can set your date in milli seconds. To prevent future dates use setMaxDate
Use getDatePicker to get the datepicker and set calendarView as told above.
private int myear; //Declare these three variables in MainActivity and call showDialog(1)
private int mmonth;
private int mday;
final Calendar myCalendar= Calendar.getInstance();
private void setCurrentDateOnView() { //Call this method before showDialog
myear = myCalendar.get(Calendar.YEAR);
mmonth = myCalendar.get(Calendar.MONTH);
mday = myCalendar.get(Calendar.DAY_OF_MONTH);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
DatePickerDialog startDate = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday){
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
}
};
myCalendar.set(myear, mmonth, mday);
long startTime = myCalendar.getTimeInMillis();
startDate.getDatePicker().setMinDate(startTime - 1000);
return startDate;
}
return null;
}