Search

Tuesday, August 4, 2009

how to update or refresh Dialog in android for second time

recently i used datepicker Dialog in my application but i got into a weird problem the datepicker dialogue was not updating for second time even though i called the showDialog and after searching on the net in vain i finally found out the solution in Android documents and partial method on one website and finally tailored it and got it worked.

onCreateDialog will only create the dialogue for first time but if we want to update it for second or consecutive time we have to write onPrepareDialog which is automatically called by showDialog below is the sample snippet for datepicker and this is similar for almost all the different dialogs

showDialog(calldailogue);// calling date picker dialogue

Below is the code for calling datepicker first time

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case calldailogue:
return new DatePickerDialog(this, yourlistener, year, month,
day);
}

return null;
}


but in case you want to update the datepicker view and two show different value each time you have to use onPrepareDialog as showDialogue will not call onCreateDialog second time it calls onPrepareDialog below is the code to refresh datepicker for second time

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case calldailogue:

((DatePickerDialog) dialog).updateDate(year, month, day);
}
}

when i came across this issue for first time it really took 3hrs to figure it out and fix the issue as i did not find this information anywhere on the web

12 comments:

Anonymous said...

tnx dude i've been searching the net for the same thing...very helpfull!!

Anonymous said...

thanks for the solution, it was very usefull

Anonymous said...

thanks! I spent 1 hour searching it :-)

Jay Khimani said...

Thanks Manjunath... This was really a great help. Was facing the same issue with ProgressDialog

Anonymous said...

Finally.. thanks!! :)

seric said...

Thank you mate!

Anonymous said...

Thanks it saved me. I spent one hour searching on my own then google and got there.

Anonymous said...

Thank you a lot!!!

Anonymous said...

you're a legend. Nice one

Bruno said...

Finally I finded de solution, thanks guy!!!

Dani said...

thanks!

anitaa said...

Thank you so much..I was searching for a solution to this for 2 hours..

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google