How do you get a DatePicker from a DatePickerDialog? - android

I have a activity that is popping up a android.app.DatePickerDialog.
DatePickerDialog dialog = new DatePickerDialog(this, startDateSetListener, start_cal.get(Calendar.YEAR), start_cal.get(Calendar.MONTH), start_cal.get(Calendar.DATE));
I want to get at the DatePicker View from the DatePickerDialog so that I can set the min and max dates. The API reference says that there is a method getDatePicker and that has been there since API 1 but it is invalid, my ide doesn't like it.
DatePicker picker = dialog.getDatePicker();
"The method getDatePicker() is undefined for the type DatePickerDialog"
Any one know what API was the method created in or how I can get to the DatePicker to edit min/max dates?
I am using API 7, Platform 2.1.

Despite that getter method only being available in Honeycomb and above (as dmon correctly pointed out), you can still fairly easily get access to the DatePicker object encapsulated by the DatePickerDialog using reflection. It'll look something like this:
DatePickerDialog dialog = ... // create dialog
Field mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
DatePicker mDatePickerInstance = (DatePicker) mDatePickerField.get(dialog);
From this point on, you have a reference to the internal DatePicker object and you should be able to change it to your own liking. Please do beware of any obvious typos, as I entered this directly into the browser.

You misread, it's only available since API 11:
"public DatePicker getDatePicker () Since: API Level 11". Look closely.

Related

Android API 21 22 datepicker year selection bug?

I've implemented the "calendar view", i.e., lollipop version of Android's Datepicker.
There's a difference between Android API 21 & 22 in the year selection of said datepicker when I click on "2015" to change the year:
using API21
using API22
I'm using a Nexus 5 simulator. I'm only seeing the current (2015) year entry in API22, whereas in API21, I can scroll through from ~1950 to ~2100. Same code and, as far as I can tell, same simulation settings.
I'm wondering if this is just a bug, if anyone else has encountered it, or has any tips for a workaround? I really like the calendar view that Lollipop offers so preferable if I can get this working right. Will update with any new information I find.
Found a quick fix. Programmatically set the min and max dates of the DatePickerDialog object. I did so in the DatePickerFragment static class definition, so every instance of DatePickerDialog gets set:
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), YourActivityHere, year, month, day);
datePickerDialog.getDatePicker().setMinDate(datePickerDialog.getDatePicker().getMinDate());
datePickerDialog.getDatePicker().setMaxDate(datePickerDialog.getDatePicker().getMaxDate());
You can also set this if you defined DatePickerDialog in an XML activity, but I'm not sure if the bug exists there.

Looking for the source code of the new Google Calendar Time and Date Picker Widgets

I would like to use the brand new Date Picker and Time Picker that is now part of the new official Google Calendar App.
http://googlesystem.blogspot.de/2013/05/new-google-calendar-controls-for-android.html
All I found so far is this source code on grepcode but I can't find the widgets or fragments or activities there.
http://grepcode.com/snapshot/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2.2_r1/
Is that already public or is there something similar out there?
I think the code you are looking for can be found here:
https://android.googlesource.com/platform/frameworks/opt/datetimepicker/+/master/src/com/android/datetimepicker
There is some pretty directly usable source code snippets in this Android Developer Pages:
http://developer.android.com/guide/topics/ui/controls/pickers.html
I used it as-is and all works just fine.
Also, reference these:
DatePickerDialog
http://developer.android.com/reference/android/app/DatePickerDialog.html
TimePickerDialog
http://developer.android.com/reference/android/app/TimePickerDialog.html
DialogFragment
http://developer.android.com/reference/android/support/v4/app/DialogFragment.html

How to localize Android DatePicker?

I put the DatePicker on my Activity. Everytime I launch my app I see english version of date in the DatePicker widget: Oct 15 2012. But I want to have a local version of date (russian) when I start my Activity. I've tried to define the locale before UI initialization. But it doesn't help. Is there any possibility to make it without extending DatePicker class?
In fact there is no need to override this class. But when I started to do it, I found info about special method "reorderpickers" which is specially designed to localize datepicker (automatically). My mistake was in setting Locale to English in the previous activity (to define English locale for password). That's why the standard method didn't work. So anybody who has the same problem check your code whether you have changed Locale.
You should create your custom datepickerdialog. For make this, that link help you;
http://coffeebreak.hiq.se/2011/09/09/fixing-androids-datepickerdialog/
Additional information for DataPicker and application locale you can find here

Android: I can't find getFullYear()

I'm using the java Date Class in eclipse for Android as getYear is soon to be deprecated and replaced with getFullYear but the Class doesn't have the getFullYear function... What do I need to do to get it??
Use
Calender statcal = Calender.getInstance(Locale locale)
and then access the field statcal.get(Calendar.YEAR). That should do it.
If you are just looking to use the result for display purposes, I would recommend using the android.text.format.DateFormat class in android.
http://developer.android.com/reference/android/text/format/DateFormat.html
You can simply do
DateFormat.format("yyyy", dateObject);
The dateObject can be a Calendar, Date or timeInMillis

Android Calendar View for Date Picker

I'm writing my first app, and I have a question about DatePicker.
My app requires the user to input a date. The most user-friendly way would be to popup a calendar-like widget that displays the current month like a calendar grid - something like this:
I want to use that in place of the DatePicker interface - which has Month, Day, and Year fields, each with an up and down button to increment/decrement.
Is this type of functionality built into any Android widget or view, or would I have to design my own custom component to do this? I figured this would already exist, seeing how much this type of UI is used so frequently in non-mobile apps and web pages.
Thanks!
Now, in 2014, even the native DatePicker (link) contains small Holo looking CalendarView (link) to pick a day in month.
You can choose, if both spinners and CalendarView or just one of them is displayed by setting:
android:calendarViewShown
android:spinnersShown
I'm not sure if it's just API level 16+ or if it was even in Ice Cream Sandwich, but it's there. This is how it looks by default:
Moreover, on API level 21 and higher there is a new Material themed DatePicker that looks like following:
This is default on API 21+ and there are no spinners anymore, but you can switch back to the Holo one by setting
android:datePickerMode="spinner"
in your XML.
Since the API 11 there natively: CalendarView
This View is in HoloEverywhere since API 7.
Is this type of functionality built into any android widget or view, or would I have to design my own custom > component to do this?
There is no component for that in the Android SDK, sorry. The widget you illustrate is too small for a touchscreen. You can implement something larger (see the Calendar app), but you are largely on your own for that.
what i found so far:
http://code.google.com/p/android-calendar-view
http://code.google.com/p/openintents/wiki/CalendarPickerAPI
I have recently written exactly this as a modular app. Here is some sample code, documentation with screenshots, and .apk download.
Found a good implemetation in http://caughtinthemobileweb.wordpress.com/2011/06/20/how-to-implement-calendarview-in-android/
Also Since API level 11 (Android 3.0) there has been the native implementation of the CalendarView http://developer.android.com/reference/android/widget/CalendarView.html
Try to use this component:
https://github.com/truefedex/android-date-picker
If you want to use it like popup write on your onclick:
if (calendarPopup == null) {
calendarPopup = new PopupWindow(getContext());
CalendarPickerView calendarView = new CalendarPickerView(getContext());
CalendarNumbersView calendar = (CalendarNumbersView) calendarView.findViewById(com.phlox.datepick.R.id.calendar);
calendar.setShowDayNames(false);
calendarView.setListener(onDateSelectionListener);
calendarPopup.setContentView(calendarView);
calendarPopup.setWindowLayoutMode(
MeasureSpec.makeMeasureSpec(llCalendar.getWidth(), MeasureSpec.EXACTLY),
ViewGroup.LayoutParams.WRAP_CONTENT);
calendarPopup.setHeight(1);
calendarPopup.setWidth(llCalendar.getWidth());
calendarPopup.setOutsideTouchable(true);
}
calendarPopup.showAsDropDown(llCalendar);
you can use this lib for date selection
https://github.com/square/android-times-square/

Categories

Resources