I want to disable past time numbers in TimePicker. In DatePicker there is a special property for disabling past dates and there is no one for TimePicker so I need to extend an original TimePicker. As I understand I need to change a layout. How to get access to the layout?
Important: android/frameworks/base/core/res/res/layout/time_picker.xml is not an answer - it has got a reference to TimePicker widget where there are no links to other layout files.
Instead of extending a timepicker, extend from dialogfragment and on your onCreateDialog() method return a timepocker dialog. Also remember to implement OnTimeChangedListener so that you can validade the selected date as you need. If you're creating a picker thats gonna be used throughout your whole application you should also consider overriding onDismiss() and then pass the selected date to the activity which started it. Hope this helps
Related
I created a view that has CalendarView added programatically.
Code:
CalendarView calendarView = new CalendarView(context);
calendarView.setDate(new Date().getTime());
cardInner.addView(calendarView);
http://www.screencast.com/t/ugGKqI2V0EH
The current set up is the one on the screenshot. I was wondering if there is a way to make it look this way?
http://screencast.com/t/Q1kwyib0hOv
This is not possible using the CalendarView widget from AOSP. See https://developer.android.com/reference/android/widget/CalendarView.html
It is however possible to prevent the user from picking any date other than a date inside a certain week. You can achieve this by using the setMinDate and setMaxDate methods: https://developer.android.com/reference/android/widget/CalendarView.html#setMaxDate(long)
A third option would be extending the CalendarView widget and altering the onDraw method to only draw one row.
Also, try and search around on google. There might be a 3rd party library that can already do this.
When should I use say a DatePickerDialog over a DatePicker widget inflated in a DialogFragment? I've always done it the second way and have no idea when/how to use the first method. The same question applies to other picker widgets like TimePicker and TimePickerDialog as well.
Difference between DatePickerDialog and DatePicker
DatePicker - It is a Control by itself.
DatePickerDialog - It is a dialog with a date picker
When should I use ?
Use DatePicker control in your activity layout (in case you have enough space)
Use the DatePickerDialog when you don't have enough space and/or if you want to open the date dialog in a separate view.
As per the doc, DatePickerDialog is a
A simple dialog containing an DatePicker.
PS. In my experience, I would avoid using it the DatePicker in the activity layout straight away because of space constraints. Consider devices like nexus 4 when you use them.
I have a datepicker from here that I have to enable and disable with a button click. I tried:
datepicker.setActivated(false);
datepicker.setEnabled(false);
datepicker.setClickable(false);
datepicker.setFocusable(false);
datepicker.setFocusableInTouchMode(false);
but nothing works. I even tried putting the datepickerdialog inside a layout and put the same settings on the layout but didn't work as well.
Thanks in advance.
Make sure that you declare datepicker object globally bind this datepicker object inside onCreate() using findViewById() method.
I got it to working this way. To disable the datepicker, select the date you want to display and
datePickerView.updateDate(year, month, day);
datePickerView.setMinDate(dateObject.getTime());
datePickerView.setMaxDate(dateObject.getTime());
To enable it back, set it accordingly to default or previous values.
I am using a DialogFragment to show a DatePicker to choose a date. The point is that such date could be optional, so somewhere i should display a checkbox asking to use the date or not. So how would you do such an UI ?.
So far i tried adding a checkbox on the same DialogFragment but i believe the interaction wont be very intuitive because if you choose the checkbox "no date" i would have to block or disable the datepicker but i wasn't able to do that and even if i do i am not sure if it is the best approach.
Have you seen any application solving a problem like this and how does it solve it ?
Here's how I solved it in my app: make it clear on the button that launches the DialogFragment that a date is optional.
After a date is chosen, add the option to remove it. Clicking on the "x" restores the button to look like above.
Is there any way to make DatePickerDialog not dismiss after clicking set button ?
Add an OnClickListener for the dialog button and override onClick.
It would be easier to create your own dialog which uses a DatePicker widget in it. You can find an example of how to use the DatePicker widget in your sdk folder under:
ApiDemos/src/com/example/android/apis/view/DateWidgets1.java
ApiDemos/src/com/example/android/apis/view/DateWidgets2.java
Then you can put whatever buttons and actions you want around the date picker.