Date Picker alert Dialog - android

When i am running my code in android 3.2 and android 4.0.3, it shows different date pickers..i want date picker as a wheeler like 1st image..i know 2.3.3 shows old pickers..But i don't know why i am getting older one in 4.0.3 which is giving new in 3.2. It happened same with Number Picker also.
here is my code:
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
if (picker == 1) {
final long lDate = mDay;
dateStart.set(Calendar.YEAR, mYear);
dateStart.set(Calendar.MONTH, mMonth);
dateStart.set(Calendar.DATE, mDay);
Log.d("startdate", "" + dateStart);
if (lDate != -1) {
UpdateStartDateTimeView();
return;
}
}
updateDisplay();
}
};

If you use the native picker, automatically Android will choose the corresponding to the current version. You can only ensure that a unique dialog is presented regardless Android version by creating a custom Dialog. Thus, you need to see if the unique picker worths the effort of designing a custom one..

The DatePickerDialog is having a constructor which accepts a theme
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
Just update ur code with
for light holo theme
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
or
for dark holo theme
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
android.R.style.Theme_Holo__Dialog_NoActionBar,
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);

If I understand you right, you're getting an old dialog style in Andoid version above 4. I had this problem when I set STYLE_NO_FRAME for dialog fragment. In this case date picker has always old design, and I don't know why it is so. If you want to get always modern UI, be sure you set STYLE_NORMAL to your dialog fragment. For do this you need to add following line in OnCreate callback method of the dialog fragment.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, 0);
}

Related

DatePickerDialog display bug on Samsung galaxy S7 run Android 7.x

Only Samsung s7 run android 7.x
Here my code
DatePickerDialog datePickerDialog = new DatePickerDialog(activity
,new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year
, int monthOfYear
, int dayOfMonth) {
listener.onDateSet(year, monthOfYear, dayOfMonth);
}
}
,c.get(Calendar.YEAR)
, c.get(Calendar.MONTH)
, c.get(Calendar.DAY_OF_MONTH));
int firstDayOfWeek = TimeUtils.getFirstDayOfWeek(activity);
datePickerDialog.getDatePicker().setFirstDayOfWeek(firstDayOfWeek);
datePickerDialog.show();
Someone advise me how to solve this problem?
Thanks!
You don't set custom style for dialog so this normal(default style). If you want "fix" this "bug" you must create custom style dialog.
For me works fine on the same device, I extend DialogFrament in my app and use code like this:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
and then use this method on the view I need to open the dialog:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
Try to implement this or follow the guide on the developer site:
https://developer.android.com/guide/topics/ui/controls/pickers.html
The problem is that the actual implementation of DatePickerDialog's UI is device specific and it can have some issues.
I've got similar issues on devices with the screen width smaller than 360dp. Fixed this by using THEME_HOLO_LIGHT theme.
DatePickerDialog has a constructor which accepts a theme:
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
You may use one of the following:
AlertDialog.THEME_DEVICE_DEFAULT_DARK
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
AlertDialog.THEME_HOLO_DARK
AlertDialog.THEME_HOLO_LIGHT
AlertDialog.THEME_TRADITIONAL

DatePicker date set in android

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.

DatePickerDialog with theme Holo Light?

How is it possible to get a DatePickerDialog with Holo Light theme?
When creating a DatePickerDialog as follows:
DatePickerDialog dpd = new DatePickerDialog(new ContextThemeWrapper(this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar),
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
or with theme android.R.style.Theme_Holo_Light or android.R.style.Theme_Holo_Light_Dialog, I get a date picker with a standard title and standard buttons. I tried to use a custom theme with a holo light parent too, but it didn't work either. It seems to work with theme android.R.style.Theme_Holo, but the result is a dark background (as expected), but I would like to have a light one.
The application's android.jar is of version 14, the application is running on a divice with android version 3.2.
I have seen an example here: http://as400samplecode.blogspot.com/2011/10/android-datepickerdialog.html, which shows a DatePickerDialog with the holo light theme, the way I would like to have it. I don't know why it doesn't work with my setup.
Thank you for help.
The DatePickerDialog has a constructor which accepts a theme
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
just update your code to include the style you want without the need for a ContextThemeWrapper
DatePickerDialog dpd = new DatePickerDialog(this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
It's working for me that way.
Read this Android DatePickerDialog example code which includes how to use different Themes in DatePickerDialog.
Here is the such one.
DatePickerDialog Constructor which support to define Theme.
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);
return new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK, this, year, month, day);
For Material style this worked for me:
int datePickerThemeResId = 0;
if (android.os.Build.VERSION.SDK_INT >= 21) {
datePickerThemeResId = android.R.style.Theme_Material_Light_Dialog;
}
new DatePickerDialog(
context,
datePickerThemeResId,
(view, year, month, dayOfMonth) -> {},
year,
month,
day
).show();

How to stop the Date Picker Dialogue-box Date to set the previous Dates. It should allow only above System current date date

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;
}

facing problem with date picker in android

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
}
});

Categories

Resources