changing screen after ok button is pressed in dialog box - java

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.

Related

How to make radio buttons display a text in another activity when clicked in android studio?

I have 6 options that the user can select from and a button that takes them to the next page when clicked. I have two pages like this. After one choice from each page is selected, I would like to display certain text depending on the radio buttons clicked previously, in another activity. How can I do this in java in android studio?
If you are controlling the FragmentManager you can just pass the options as an argument to the next Fragment's constructor otherwise, you can save everything in a static variable (as long as no Views are in there, so don't store the radio buttons there) and access that variable from outside.
Like this:
public class MyFragment {
public static boolean isRadioButtonXPressed = false; //change to true if it's pressed by default
public void onCreate(...) {
radioButtonX.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isRadioButtonXPressed = checked;
}
});
}
}
//from the other fragment
public class MyFragment2 {
public void onCreateView(...) {
if (MyFragment.isRadioButtonXPressed) {
//it's been pressed
} else {
//it's not been pressed
}
}
}
There are many different ways.
As you are using activity, you can use Intent to pass data. Here is a sample:
Intent page = new Intent(FirstActivity.this, SecondActivity.class);
page.putExtra("key", "This is my text");
startActivity(page);
For getting the value on Second Activity onCreate() method:
String value = getIntent().getStringExtra("key");

How to add buttons to the JFace ErrorDialog

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);

Jface Dialog, How to retrieve correctly what button pressed the user?

I'm having troubles with a custom Dialog in Eclipse.
in the first place, I created a Class that extend Dialog.
public class ModificarGrupoBCDialog extends Dialog {
private static final int CANCELAR = 999;
private static final int MODIFICAR = 1;
...
somewhere I create the buttons...
protected void createButtonsForButtonBar(Composite parent) {
this.createButton(parent, MODIFICAR, "Modificar", true);
this.getButton(MODIFICAR).setEnabled(puedeAltaGrupoBC());
this.bt_ok = this.getButton(MODIFICAR);
this.createButton(parent, CANCELAR, "Cancelar", false);
Display display = window.getShell().getDisplay();
Image image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/modificar.png"));
this.getButton(MODIFICAR).setImage(image);
image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/cancelar.png"));
this.getButton(CANCELAR).setImage(image);
}
and when the user clicks...
protected void buttonPressed(int buttonId) {
switch (buttonId) {
case MODIFICAR:
// Some Code, for Change Button
break;
case CANCELAR:
setReturnCode(CANCELAR);
close();
break;
}
Finally, this is how I open and get the returnCode, in the caller object.
...
ModificarGrupoBCDialog modificarGrupoBC = new ModificarGrupoBCDialog(window.getShell(), window, gr_bc);
if (modificarGrupoBC.getReturnCode() == Window.OK) {
//... Some code on OK
} else {
//another code when cancel pressed.
}
;
as you can see, after trying a while, I have to write setReturnCode() in CANCELAR switch block, is that OK ?
I spect that Dialog class automatically asign the correct return code.
May be someone could point me to a good sample.
I'm reading Vogela's blog, and may be the solution is to override okPressed() method ?
Best Regards.
The standard dialog sets the return code in two places:
protected void okPressed() {
setReturnCode(OK);
close();
}
protected void cancelPressed() {
setReturnCode(CANCEL);
close();
}
so your code doing:
setReturnCode(xxxx);
close();
should be fine as long as the button id you are using does not match the Cancel or OK button ids.
You could also use the approach used by MessageDialog which simply does this:
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
close();
}

How to set Dialog result while clicking X - close window button in ControlsFX

I extended ControlsFX Dialog to create a custom ValidationDialog. I added some custom buttons where, one of them trigger a validation operation after clicking on it. If validation is passed, the dialog is closed with the OK result, but if dialog is not valid, a dummy Action objech is assigned to a dialog result action. It purpose (dumy result) is just to save information that user tried to fill dialog, and allow him to make some corrections (not to close window). Everything would be ok, but there is one scenario which creates wrong result. If user will fill form with an error data, than click validation button and after that he will click X button (close window). After those steps the dialog result will be still a validation button. I tried to bind some listener to window onCloseRequest() property, but it is not reacting. How to solve this issue?
Below is a shortened version of my code:
public class ValidationDialog extends Dialog {
private ValidationDialog thisDialog;
public final Action DUMMY_ACTION = new AbstractAction("OTHER") {
{
ButtonBar.setType(this, ButtonBar.ButtonType.OTHER);
}
#Override
public void execute(ActionEvent actionEvent) {
//do nothing
}
};
public final Action VALID_OK = new AbstractAction("OK"){
{
ButtonBar.setType(this, ButtonBar.ButtonType.OK_DONE);
}
#Override
public void execute(ActionEvent ae) {
if (isValid()) {
thisDialog.setResult(this);
thisDialog.hide();
} else {
thisDialog.setResult(DUMMY_ACTION);
}
}
};
public ValidationDialog(Object owner, String title) {
super(owner, title);
thisDialog = this;
}
private boolean isValid(){
return false;
}
}
and it's call:
ValidationDialog validationDialog = new ValidationDialog(stage, "Fill form");
Action result = validationDialog.show();
if (result.equals(validationDialog.VALID_OK)){
//do important stuff
}
One would have expected that clicking X will always mean cancel.
So far the only way is to create own boolean variable like "valid" with getter, then set it before hide().
outside do:
if (dlg.isValid()) {
//dlg success
} else {
//dlg fail
}

how to detect a search button while an alert is shown in an android

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.

Categories

Resources