I'm trying to add a "Cancel" button to this popup dialog, the dialog basically just gives the user some info and allows them to hit Yes or view details. The problem is that there is no Cancel button and I would like to add one.
The dialog is a JFace ErrorDialog which uses a premade MultiStatus to display the error message. The dialog opens and gives an OK button or a Cancel button. Is there anyway to directly manipulate how the dialog creates buttons or some other method I could use to change how it looks? Any help is appreciated!
if (ErrorDialog.openError(shell,
Messages.ConsistencyAction_confirm_dialog_title, null,
multiStatus, IStatus.WARNING) != Window.OK) {
return;
}
This is the dialog I'm trying to change. This is basically checking to make sure that someone presses ok, if they don't then you exit. You can exit it by hitting the red X in the corner but it'd be less confusing to have a button.
You can extend the ErrorDialog class so that you can override the createButtonsForButtonBar method.
For example this is from the Eclipse p2 install plugin:
public class OkCancelErrorDialog extends ErrorDialog {
public OkCancelErrorDialog(Shell parentShell, String dialogTitle, String message, IStatus status, int displayMask) {
super(parentShell, dialogTitle, message, status, displayMask);
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
// create OK, Cancel and Details buttons
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
createDetailsButton(parent);
}
}
With this you can't use the static ErrorDialog.openError method, instead you will have to do something like:
OkCancelErrorDialog dialog = new OkCancelErrorDialog(shell, Messages.ConsistencyAction_confirm_dialog_title, null, multiStatus, IStatus.WARNING);
Related
I was trying to show a success message with dialog box to user before changing the screen. And I want it to wait for user to click the ok button or press Enter key and then change the screen. Since I have to put lots of dialog boxes in my program, to avoid duplicates, I tried to have one createDialog method in my MainClass which creates the dialog boxes and it will add it to the stage which I passed to the method. But the thing is I want it to change the screen to the one I passed to it after the ok button was pressed by user but dialog's result function is an inner method which doesn't access the Screen which I passed to the function. So is there any way that I can do this?
public class MainClass extends Game {
.
.
.
public void createDialog(String message, boolean isWarning, Stage stage, Screen screen) {
Skin skin2Json = new Skin(Gdx.files.internal("freezing/skin/freezing-ui.json"));
Dialog dialog;
String title = "Success Message";
if (isWarning) title = "Error";
dialog = new Dialog(title, skin2Json, "dialog"){
#Override
protected void result(Object object) {
if((Boolean) object)
//if it is a success message ,I want to set screen to the screen passed to the createDialog function
}
};
dialog.getBackground().setMinWidth(400);
dialog.getBackground().setMinHeight(200);
dialog.text(message);
dialog.button("Ok", true);
dialog.key(Input.Keys.ENTER, true);
dialog.show(stage);
}
}
Any help is appreciated.
There are different ways you could do that I guess, but mine would be :
to use a variable in your class, in the render function, that triggers the change of screen
to set that variable once you hit the OK in the dialog box.
That would give something like :
public class MainClass extends Game {
private Boolean ChangeScreen = false;
public void render () {
// This is your typical render function
if(ChangeScreen) Game.setscreen(new MyOtherScreen());
}
public void createDialog(String message, boolean isWarning, Stage stage, Screen screen) {
Skin skin2Json = new Skin(Gdx.files.internal("freezing/skin/freezing-ui.json"));
Dialog dialog;
String title = "Success Message";
if (isWarning) title = "Error";
dialog = new Dialog(title, skin2Json, "dialog"){
#Override
protected void result(Object object) {
if((Boolean) object)
// The OK button has been hit
ChangeScreen = true;
}
};
dialog.getBackground().setMinWidth(400);
dialog.getBackground().setMinHeight(200);
dialog.text(message);
dialog.button("Ok", true);
dialog.key(Input.Keys.ENTER, true);
dialog.show(stage);
}
}
Maybe not the most elegant way but it should do the trick.
In my code:
#Override
public void onBackPressed() {
}
It works fine, but if I call a dialog, for example
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
I can use the back button (the back button close the dialog popup). But I want to disable the back button in all layouts and dialogs.
You should override onBackPressed in all of your activities and for Dialog you can use setCancelable(false) like:
final Dialog dialogPopupGewonnen = new Dialog(Start.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialogPopup.setContentView(R.layout.popup);
dialogPopup.setCancelable(false);
I have a custom JFace Dialog (called PropertyDialog) which extends the FormDialog. I would like to open a modal Message Dialog over the PropertryDialog as soon as it opens, to display a message to the user.
How could this be accomplished? Would I have to override the open() method? Note that it is required that the PropertyDialog.open() does not return until a button is pressed on the button bar.
Thanks for your help.
You can do this by displaying the message at the end of the createContents method, like this:
#Override
protected Control createContents(final Composite parent)
{
final Control control = super.createContents(parent);
parent.getDisplay().asyncExec(new Runnable() {
public void run()
{
MessageDialog.openInformation(getShell(), "title", "message");
}
});
return control;
}
You need to use Display.asyncExec so that the dialog is not displayed until the parent dialog has been displayed.
i want to have in my application an alertdialog, that has its message updated everytime it is showed.
This is because the dialog box value depends on some values on the application.
Now i tried to use the showDialog method:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
showDialog(RESULT_DIALOG);
return false;
}
But once the dialog is created, it doesn't change the message (i know that if the dialog is created, it use the started version).
My onCreateDialog method code is:
public Dialog onCreateDialog(int dialogId) {
AlertDialog dialog;
switch(dialogId) {
case RESULT_DIALOG:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(localTv.getText())
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
There is a way to update the content of the AlertDialog.
Actually i create a new dialog box every time the onTouch event is called. But i'm not sure that it is the cleanest way to solve that problem.
Any idea?
Thanks :)
You have to use onPrepareDialog method:
#Override
protected void onPrepareDialog ( int id, Dialog dialog ) {
switch ( id ) {
case RESULT_DIALOG:
AlertDialog alertDialog = ( AlertDialog ) dialog;
alertDialog.setMessage( localTv.getText() );
break;
}
super.onPrepareDialog( id, dialog );
}
From http://developer.android.com/guide/topics/ui/dialogs.html :
Before the dialog is displayed, Android also calls the optional
callback method onPrepareDialog(int, Dialog). Define this method if
you want to change any properties of the dialog each time it is
opened. This method is called every time a dialog is opened, whereas
onCreateDialog(int) is only called the very first time a dialog is
opened. If you don't define onPrepareDialog(), then the dialog will
remain the same as it was the previous time it was opened. This method
is also passed the dialog's ID, along with the Dialog object you
created in onCreateDialog().
You can always change the dialog using onPrepareDialog or you can remove the dialog (so it will always pass through onCreateDialog) setting the onDismiss (dialog.setOnDismiss) to remove the dialog id (removeDialog(id)).
Hi stackoverflow friends
I recently faced an issue that how can i disable global search button in android while an alert is shown in the screen.I don't want to disappear the alert box by using search button. I need to user must click the alertbox button and disappears in that way.So I want to disable the search button while alert box is shown. But I can disable the back button using setCancable(false).How can I solve this ?
THanks in advance.
So, Your intention is to provide non-cancelable alert.
Suggesting to set OnDismissListener and just show alert again. It's not very good from visual perspective (alert get closed and opened again).
Below is some obvious example how to achieve such non-cancelable alert (code is inside Acctivity class):
/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;
#Override
protected void onResume() {
// Say, we need to show alert when activity resumed
if(true/*provide condition to show alert*/) {
showAlert();
}
}
/**
* Show non dismissable alert
*/
private void showAlert() {
if(null == this.alert) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.str_alert_title);
builder.setMessage(R.string.str_alert_text);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
YourActivity.this.alertKeyPressed = true;
dialog.dismiss();
}
});
this.alert = builder.create();
this.alert.setOwnerActivity(this);
this.alert.show();
this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// dialog is not allowed to be dismissed, so show it again
YourActivity.this.alert = null;
if(!YourActivity.this.alertKeyPressed) {
showAlert();
}
}
});
}
}
However, I don't think it's the right way to left such alert for the user, sometimes it might be needed for cases like evaluation restriction etc.
Override onSearchRequested in your Activity and have it return false while the dialog is being shown. This should block the request, as per the docs:
You can override this function to force global search, e.g. in
response to a dedicated search key, or to block search entirely (by
simply returning false).
Returns true if search launched, and false if activity blocks it. The
default implementation always returns true.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//There you catch the key, do whatever you want to do.
//Return true if you handled the key event, so nothing will trigger.
//Return false if you want your activity to handle.
return true;
}
})
Just add the code above to alert dialog builder. Hope this snippet would help. Good luck.