Android: How to Get a Modal Dialog or Similar Modal Behavior

modal dialog in Android

I believe you will find your answer here

Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

In general you should not be blocking the code while a dialog is visible, your logic should be different and fit android way of doing things.

How to make android dialog modal?

Surely, you can set up onPositiveButton click listener for your dialog and do your action from that listener.

If you indeed want to pause your activity at a certain point you, probably, can use the old java wait/notify mechanism or more convenient new mechanisms such as executors.

Why would you want to do that though is not clear. Android dialogs are specifically designed to be non-modal so that they can in no way block your application (as your application can include other activities such as native Phone Call activity and it would be bad if those get blocked).

How to create modal dialog box in android

There are many kind of Dialogs in Android. Please take a look at Dialogs. I guess what you are looking for is something like AlertDialog . This is the example of how you can implement on BackPress button.

@Override
public void onBackPressed() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Do you want to logout?");
// alert.setMessage("Message");

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Your action here
}
});

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});

alert.show();

}

Is there a simple way to know if a modal dialog is currently displayed?

(This is what I know and works, though I'm not sure if it's correct to use Window#isShowing, or if I should use something else.)

public static boolean isModalDialogShowing()
{
Window[] windows = Window.getWindows();
if( windows != null ) { // don't rely on current implementation, which at least returns [0].
for( Window w : windows ) {
if( w.isShowing() && w instanceof Dialog && ((Dialog)w).isModal() )
return true;
}
}
return false;
}

Fake modal dialog using Show?

I have actually almost implemented local modal dialogs now.
It is built around that when a TForms Enabled property is set To False the whole Form is locked from input. And my modules is just a descendant from TForm.

My ViewManager class that decide what modules is current add/close modules etc got 2 new methods. LockCurrentView and UnLOckCurrentView.

function TViewManager.LockCurrentView: TChildTemplate;
begin
Result := CurrentView;
Result.Enabled := False;
Result.VMDeactivate; // DeActivate menus and toolbas for this module
end;

procedure TViewManager.UnLockCurrentView(aCallerForm: TChildTemplate);
begin
aCallerForm.VMActivate; // Activate menus and toolbas for this module
aCallerForm.Enabled := True;
end;

TAttracsForm is the baseclass of all dialogs. I Override FormClose and add a new method ShowLocalModal to call instead of ShowModal. I also have to add a TNotifyEvent OnAfterDestruction to be called when the dialog is closed.

procedure TAttracsForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(fCallerForm) then
begin
ClientMainForm.ViewManager.UnLockCurrentView(fCallerForm as TChildTemplate);

if Assigned(OnAfterDestruction) then
OnAfterDestruction(Self);

Action := caFree;
end;
end;

{ Call to make a dialog modal per module.
Limitation is that the creator of the module must be a TChildtemplate.
Several modal dialogs cannot be stacked with this method.}
procedure TAttracsForm.ShowLocalModal(aNotifyAfterClose: TNotifyEvent);
begin
fCallerForm := ClientMainForm.ViewManager.LockCurrentView; // Lock current module and return it
PopupParent := fCallerForm;
OnAfterDestruction := aNotifyAfterClose;
Show;
end;

Some test with simple dialogs looks promising. So the module just have to call ShowLocalModal(myMethod) which have a TNotifyEvent as parameter. This method is called when the dialog is closed.

Dialogs / AlertDialogs: How to block execution while dialog is up (.NET-style)

Ted, you don't want to do this, really :) The biggest reason is that if you block the UI thread while you are displaying a Dialog, you will block the thread that's in charge of drawing and handling the events of your Dialog. Which means your dialog will be unresponsive. You will also cause ANRs if the user takes more than a few seconds to click the dialog.

Erich's answer is exactly what you need. I know it's not what you want, but that doesn't matter. We've designed Android to prevent developers from writing synchronous dialogs so you don't really have much of a choice.



Related Topics



Leave a reply



Submit