0

Times ago I developed a custom datetime picker control. This custom picker control was developed by inheriting from LinearLayout class and has got some features like "declare-styleable" attributes to set properties at design-time, a set of Interfaces to simultate events and finally a large amount of code to play effects and manage date-time business logic.

Now my need is to show a DatePickerDialog and TimePickerDialog by pressing a button into my custom control. I searched on the android knowledge-base and the only method I found to show these dialogs is the Developer's DatePicker Guide.

Because of my control inherits directly from LinearLayout, I can't override onCreateDialog() method to provide the dialogs, simply because LinearLayout doesn't havethis method! How can I show these dialogs in my custom-control without any references to the parent activity? Pratically, I would like to have the opportunity to draw the control on many Activities as I can, without worrying about managing onCreateDialog() on each Activity, because the code that loads and launches the dialogs is defined once into my custom control (as you do in a simple .NET control when you have to show MessageBoxes [MessageBox.Show() doesn't require a Form instance])...

How can I achieve that? Thanks in advance...

Sam
  • 86,580
  • 20
  • 181
  • 179
GiveEmTheBoot
  • 534
  • 9
  • 24
  • What are you trying to do in onCreateDialog()? Why not just call your own function from your button, modify your dialogs, and then display the dialogs? – Sam May 25 '12 at 18:36
  • I need onCreateDialog to show the dialog. In all example i found on the web, to show a dialog, they call Activity.ShowDialog() method and then in onCreateDialog() overrided function, they build and return the Dialog. My control doesn't inherits from Activity, so there isn't any ShowDialog() and OnCreateDialog() methods. – GiveEmTheBoot May 25 '12 at 18:45

1 Answers1

3

Instead of using Activity.showDialog(), onCreateDialog(), etc; just use Dialog.show().

Calendar calendar = Calendar.getInstance();
mDialog = new DatePickerDialog(context, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
mDialog.show();

Inside your button's onClickListener, you can modify mDialog anyway you want and then call mDialog.show().

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I ll try this ;) but...i don' t understand why in the examples posted on the web, all dialogs called into an activity function are initialized and provided by onCreateDialog() method....why do in this complicated way instead of simply call the Show() function as you suggest? Sorry but im little bit novice...anyway...i ll try it! – GiveEmTheBoot May 25 '12 at 21:43
  • Tried in your simple way and it works perfectly =)...if someone can explain me why in all the example i found dialogboxes are showed by calling the Activity.ShowDialog() method, i'd be very grateful! – GiveEmTheBoot May 27 '12 at 15:34
  • 1
    It is nothing more than coincidence, there are plenty of examples that use Dialog.show(). Activity.showDialog() is depreciated even below API 11 for DialogFragment while Dialog.show() is still current. However using showDialog() will retain its current state after an orientation change (or any view invalidation), while you must add this functionality yourself if you use Dialog.show(). – Sam May 27 '12 at 17:54
  • Yep, i've got it ;)...maybe i've been so unlucky to read old posts. Anyway thanks for your help! – GiveEmTheBoot May 27 '12 at 19:12
  • I wish I would have known about this Same!! Lol. I've been doing it the lng way the whole time -_- – Andy Jun 16 '12 at 09:32