This is gonna be disappointing to you "great" finding but just call progressDialog. showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
To create a dialog window, override the onCreateDialog() protected method which is defined in the Activity base class.
Dialog DesignAlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog(). You just have to keep a reference to the Dialogs you create in onCreateDialog().
You can use flag to check whether dialog is opened or not and according to flag value you can do whatever you want. Like I did. I am having only one dialog but when I touch another EditText
and if my dialog is opened then it should close first and then should re-open with animation.
Code snippet:
private EditText mEditText, mEditCode;
private Dialog mDialog;
private int mClicked = 0;
private boolean isShown = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText)findViewById(R.id.EnterValue);
mEditText.setClickable(true);
mEditText.setOnClickListener(this);
mEditCode = (EditText)findViewById(R.id.EnterCode);
mEditCode.setClickable(true);
mEditCode.setOnClickListener(this);
}
public void onClick(View nView)
{
switch (nView.getId())
{
case R.id.EnterValue:
mClicked = R.id.EnterValue;
mEditText.requestFocus();
mEditText.setFocusableInTouchMode(false);
mEditText.setEnabled(true);
mEditText.setSelection(mEditText.getText().toString().trim().length());
if(isShown)
{
mDialog.dismiss();
showInfoDialog();
}
else
{
showInfoDialog();
}
break;
case R.id.EnterCode:
mClicked = R.id.EnterCode;
mEditCode.requestFocus();
mEditCode.setFocusableInTouchMode(false);
mEditCode.setEnabled(true);
mEditCode.setSelection(mEditCode.getText().toString().trim().length());
if(isShown)
{
mDialog.dismiss();
showInfoDialog();
}
else
{
showInfoDialog();
}
break;
}
}
private boolean showInfoDialog()
{
mDialog = new Dialog(CustomKeyboardNotLikeAndroidActivity.this, R.style.PauseDialog);
mDialog.setContentView(R.layout.keyboard);
mDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
mDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mDialog.setCancelable(true);
isShown = true;
mDialog.show();
return false;
}
Try to alter this code in your way. Hope this can help you. Thanks.
Dialog
implements DialogInterface
which has OnShowListener
.
Therefore you can use code like this:
Dialog dialog = new Dialog(context);
// ... set all things considering your dialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// do something when your dialog is shown
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With