Reputation: 1049
Is there any way to show the same dialog but with different values?
public void onClick(View view) {
Calendar currentTime = Calendar.getInstance();
final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
final int minute = currentTime.get(Calendar.MINUTE);
final String message = "Right now is" + hour + minute;
//Creates a dialog with a message about the current time
new AlertDialog.Builder(getContext())
.setTitle("ATENTION:")
.setMessage(message)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do Something
} })
.setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Shows a new dialog to pick time
TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
int hour = selectedHour;
int minute = selectedMinute;
} }, hour, minute, true);
timePicker.show();
} })
.setIcon(android.R.drawable.sym_def_app_icon)
.show();
}
After clicking "Confirm" in the timePicker dialog I want to show the first dialog again (the one with the message) but now with the new time selected. Is that possible?
Upvotes: 1
Views: 50
Reputation: 8558
yes,you can save the Dialog as a filed,for example AlertDialog mDialog
;
when you want show it,firstly check whether it's null,if null use builder.build to init it.otherwise use mDialog.setMessage(<new message>)
.
ALertDialog mDialog = null;
public void onClick(View view) {
if(mDialog == null){
mDialog = newDialog();
}else{
final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
final int minute = currentTime.get(Calendar.MINUTE);
final String message = "Right now is" + hour + minute;
mDialog.setMessage(message);
}
mDialog.show();
}
public AlertDialog newDialog(){
Calendar currentTime = Calendar.getInstance();
final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
final int minute = currentTime.get(Calendar.MINUTE);
final String message = "Right now is" + hour + minute;
//Creates a dialog with a message about the current time
return new AlertDialog.Builder(getContext())
.setTitle("ATENTION:")
.setMessage(message)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do Something
} })
.setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Shows a new dialog to pick time
TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
int hour = selectedHour;
int minute = selectedMinute;
} }, hour, minute, true);
timePicker.show();
} })
.setIcon(android.R.drawable.sym_def_app_icon)
.build();
}
Upvotes: 1
Reputation: 1033
You can use a function that returns the dialog object and then just pass the message to the function and call show.
public AlertDialog getDialog(String message){
new AlertDialog.Builder(getContext())
.setTitle("ATENTION:")
.setMessage(message)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do Something
} })
.setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Shows a new dialog to pick time
TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
int hour = selectedHour;
int minute = selectedMinute;
} }, hour, minute, true);
timePicker.show();
} })
.setIcon(android.R.drawable.sym_def_app_icon)
.show();
}
getDialog("message").show();
Upvotes: 1