My app creates an AlertDialog in which a user enters the name to save. When a user clicks the save button, the onClickListener will check for duplicated name. If the name already exists, another dialog box will pop up to alert the user that existing data will be replaced. The user then have a choice to cancel and go back to change to a new name or go ahead and have the data replace.
When the second dialog appears, I expect the first dialog box is still visible until I call dismiss. However, the first AlertDialog disappeared before the second AlertDialog appears. That is dismiss will automatically be called when a button is clicked. Is this a bug or by design?
I wrote the test case below which I checked on 3 devices: Nexus S android 4.0, HTC Rezound android 2.3 and Motorola Droid Bionic android 2.3.
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Some message will be here"
/>
<Button
android:id="@+id/show_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show"
/>
</LinearLayout>
Code
public class AlertDialogBug extends Activity
{
static final int DIALOG_ALERT_ID = 1;
AlertDialog alertDlg;
TextView message;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (TextView) findViewById(R.id.message);
Button showButton = (Button) findViewById(R.id.show_btn);
showButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showDialog(DIALOG_ALERT_ID);
}
});
}
private AlertDialog createAlertDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bug?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// No dismiss, cancel, finish, or removeDialog,
// but the dialog will disappear when this button is clicked.
}
});
alertDlg = builder.create();
alertDlg.setOnDismissListener(new OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialog)
{
message.setText("onDismiss was called");
}
});
return alertDlg;
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_ALERT_ID:
return createAlertDialog();
default:
return super.onCreateDialog(id);
}
}
}
I originally wrote the save dialog box as an activity with android:theme=”@android:style/Theme.Dialog”. The UI looks fine on the Nexus S and Rezound but look terrible on the Droid Bionic (the edit box and button only occupied half of the width, the other half is blank).
This is by design.
If you don’t want to cancel the dialog by click the button, here below is some codes for you.
Add this in your setPositiveButton method when you don’t want to cancel the dialog.
Then if you want to cancel the dialog, just need to add this below.
By the way, your xml is never called by your alert dialog. As the setTitle() and setMessage method are provided by alert dialog.
If you want to provide custom dialog, call setCustomeView(layout).
Any questiones, let me know.