# Is there a easy way to replace AlertWindow::runModalLoop()?

**URL:** <https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072>\
**Category:** Android\
**Created:** [July 3, 2015, 9:23am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072 "2015-07-03T09:23:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 3, 2015, 9:23am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/1 "2015-07-03T09:23:52Z")

</div>

Just found&nbsp;AlertWindow::runModalLoop() can't compile on Android OS.

Is there a easy way to replace it? Because I have some code using this method&nbsp;in many places.&nbsp;If I adopt DialogWindow to rewrite them one by one, it'll be a grind...

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [July 3, 2015, 1:33pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/2 "2015-07-03T13:33:47Z")

</div>

You could create a component that covers your screen with a dark semi-transparent (or completely transparent or whatever)&nbsp;background and a pseudo dialog box on top with the appropriate buttons.&nbsp;

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [July 3, 2015, 1:51pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/3 "2015-07-03T13:51:22Z")

</div>

AlertWindow::showMessageBoxAsync?

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 3, 2015, 6:02pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/4 "2015-07-03T18:02:21Z")

</div>

Thanks, phil.

... then, how do I get the return value after&nbsp;make the pseudo dislog disappeared?&nbsp;Still have to write more codes, won't I?&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 3, 2015, 6:23pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/5 "2015-07-03T18:23:18Z")

</div>

Thanks, Dave.

Yes, &nbsp;if you do not need a return value,&nbsp;AlertWindow::showMessageBoxAsync() is a better and only way to replace the&nbsp;showMessageBox() from what I know about JUCE on Android.&nbsp;I've replaced all of this method by search and replace, which haven't taken my too much time :)

I'd apologize for my&nbsp;indistinct&nbsp;description. My situation is: I need one or two return value after clicked a button, then do some operations&nbsp;internally. For example:

```
AlertWindow window (...);

window.addButton (..., 0...); 
window.addButton (..., 1...); 
window.addButton (..., 2...); 

window.addTextEditor (...,1...);
window.addTextEditor (...,2...);

window.addComboBox (...);

window.add... //(more widgets);

const int result = window.runModalLoop();

if (0 == result)

    doOneThing();

else if (1 == result)

    doAnother();

else if (2 == result)

   doSomethingElse();
```

I feel&nbsp;AlertWindow is a very handy class to do such things, so I used it nearly everywhere if it's possible. Now, I'm ready&nbsp;to pay the price... :)

&nbsp;

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 3, 2015, 6:38pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/6 "2015-07-03T18:38:26Z")

</div>

Also, I've managed to use the&nbsp;PopupMenu::showMenuAsync() and a static callback function to replace PopupMenu::show(), so I think perhaps there is a method I haven't found yet for AlertWindow&nbsp;just like PopupMenu...

---

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [July 4, 2015, 6:31am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/7 "2015-07-04T06:31:23Z")

</div>

Well it's fairly easy to get whatever a dialogBox can return and more. If you just want to know which button was pressed use the buttonClicked method. You can enter inputs with a textEditor, etc. All you have to do is create a class once for all. And then use it for multiple usages.

When a button is clicked (eg OK or Cancel or whatever you decided to use) just use setVisible(false) to remove the pseudo dialog box.

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 4, 2015, 9:47am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/8 "2015-07-04T09:47:49Z")

</div>

Yes, phil,&nbsp;this's exactly what I thought before I posted this thread: rewrite all of them&nbsp;by custom&nbsp;Components, use some of the&nbsp;Component's methods,&nbsp;then get a DialogWindow::LaunchOptions to show it up.&nbsp;However, my situation isn't so simple, I have many AlertWindows, each of them have different widgets and behavior... One even has 5 buttons, 4&nbsp;TextEditors,&nbsp;2&nbsp;ComboBoxes and a custom Component.&nbsp;:D

Like I said, I've used PopuMenu::showMenuAsync() and AlertWindow::showOkAndCancelBox (use static callback function) to modified the old codes easily, so I thought maybe there's a method perhaps could use a&nbsp;callback function or something else to&nbsp;replace AlertWindow::runModalLoops().

Now, I'm considering to write a class called AlertWindowForLoopfinesMiserable&nbsp;include the most methods of AlertWindow's, except&nbsp;the Component::runModalLoops()... :)

BTW, I think Jules should add some words to the comments of AlertWindow class, remind all JUCE programmers&nbsp;not to use runModalLoop() on any Android project....If I'm wrong again, please forgive&nbsp;it.

See:&nbsp;http://www.juce.com/api/classAlertWindow.html

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [July 4, 2015, 10:27am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/9 "2015-07-04T10:27:29Z")

</div>

showMessageBoxAsync takes a&nbsp;ModalComponentManager::Callback\* as a parameter so you can use it in a similar way to PopupMenu.

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 6, 2015, 1:03pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/10 "2015-07-06T13:03:00Z")

</div>

Thanks, guys. Finally, I got it run on Android.&nbsp;My nasty&nbsp;strategy was: wrote a new class, contains&nbsp;many methods same name as&nbsp;AlertWindow, made the buttonClicked() = 0; then&nbsp;derived child-classes for each of them...

Later on, I also&nbsp;encountered the FileChooser's modal issue, &nbsp;what I did was using FileChooserDialogBox and&nbsp;static callbacks to kick it off.

There're still many problems on Android with my project though.&nbsp;However, see it running smoothly on this 'new' OS makes me very excited. I have to say, what a amazing library JUCE is!!!&nbsp; ![cool](http://www.juce.com/sites/all/libraries/ckeditor/plugins/smiley/images/shades_smile.gif "cool")

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [July 6, 2015, 2:00pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/11 "2015-07-06T14:00:34Z")

</div>

Good work.

If you think your code is reusable for other JUCE Android developers, please consider making a JUCE module of your code :)

---

<div class="post-metadata">

**Author:** ![loopfine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/loopfine/32/141_2.png) [@loopfine](https://forum.juce.com/u/loopfine)\
**Post date:** [July 7, 2015, 10:31am UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/12 "2015-07-07T10:31:00Z")

</div>

Yes,&nbsp;adamski, would love to do that if such a trivial thing could help others :)&nbsp;I always admire man&nbsp;who shared precious experiments and skills selflessly&nbsp;to the world, give&nbsp;helps to others, just like Jules and many, many people&nbsp;from this forum.

However, you see, the most difficult thing to me, not only have I known little about JUCE, but the horrible level of my English and programming as well. A&nbsp;lot of concepts and thoughts I can't explain them clearly,&nbsp;not to mention&nbsp;accomplish them elegantly&nbsp;within C++ and JUCE. I've been learning English, C++ and JUCE whenever I have time, all of these, very interesting to play with, and also are big challenges to me... I hope one day I could do something for others. :))

---

<div class="post-metadata">

**Author:** ![marvu](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marvu](https://forum.juce.com/u/marvu)\
**Post date:** [September 5, 2016, 12:58pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/13 "2016-09-05T12:58:37Z")

</div>

Different area, same question. I’m using AlertWindows in an Audio Plugin and as far as I don’t need more than messages and buttons, I can run them with the given show… functions. But now, I have one AlertWindow using a textEditor. We should not use runModalLoop (I understand, and it’s not working anyway), but what is the right way to run it so that I can still access the return values depending on which button was clicked? Or do I have to create my custom DialogWindow? Because the AlertWindow has the nice functionality to add components such as a TextEditor, I thought there must be a good way to run it too?  
If enterModalState is the answer, I would be happy about a short example how to handle the callback…

---

<div class="post-metadata">

**Author:** ![audiooffler](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/audiooffler/32/693_2.png) [@audiooffler](https://forum.juce.com/u/audiooffler)\
**Post date:** [November 10, 2016, 2:04pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/14 "2016-11-10T14:04:22Z")

</div>

Hi folks, I’m successfully using AlertWindows without modal loops (e.g. for Android compatibility).  
I do that by constructing them, adding buttons / other components and then calling _enterModalState_. Callback Functions then handle my alert boxes.

Good examples on how to create them are listed in the documentation:  
[https://www.juce.com/doc/classModalCallbackFunction](https://www.juce.com/doc/classModalCallbackFunction)

For example by using _ModalCallbackFunction::forComponent_, I turn alertWindow-\>_getTextEditor()_ and the _this_ pointer over to the callback, to have access to both, a text entered in an alertWindow, and the calling instance from inside the static callback function.

---

<div class="post-metadata">

**Author:** ![ttg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ttg/32/14456_2.png) [@ttg](https://forum.juce.com/u/ttg)\
**Post date:** [February 10, 2020, 2:25pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/16 "2020-02-10T14:25:11Z")

</div>

Just had one issue so far which was this:

> [@Adding text field to an alert box on Android with no contents causes a crash](https://forum.juce.com/t/adding-text-field-to-an-alert-box-on-android-with-no-contents-causes-a-crash/31399/3):
>
> Year late. this still exists. really strange. Here is the ambiguous crashlog hoping it’ll improve the searchability of this thread for others. CallIntMethodV called with pending exception java.lang.SecurityException: Using null token requires permission android.permission.WRITE\_SECURE\_SETTINGS 2020-02-01 23:05:47.070 10329-10329/com.talaviram.tick A/art: art/runtime/java\_vm\_ext.cc:470] at void android.os.Parcel.readException(int, java.lang.String) (Parcel.java:1684) 2020-02-01 23:05:47.070 …

---

<div class="post-metadata">

**Author:** ![ZioGuido](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/zioguido/32/6975_2.png) [@ZioGuido](https://forum.juce.com/u/ZioGuido)\
**Post date:** [February 10, 2020, 2:29pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/17 "2020-02-10T14:29:31Z")

</div>

Here is an implementation that does work:

```
class myComponent : public Component
{
public:
	// ...

private:
	// ...
	AlertWindow *pwdDialog; // <-- Must be a class member!

	void AskForPassword()
	{
		pwdDialog = new AlertWindow("Security", "Please type your password.", AlertWindow::NoIcon);
		pwdDialog->addTextEditor("pwd", "", "", true);
		pwdDialog->addButton("OK", 1, KeyPress(KeyPress::returnKey, 0, 0));
		pwdDialog->addButton("Cancel", 0, KeyPress(KeyPress::escapeKey, 0, 0));

		pwdDialog->enterModalState(true, ModalCallbackFunction::create([this](int r)
		{
			if (r)
			{
				auto text = pwdDialog->getTextEditorContents("pwd");
				Login(pwd);
			}
		}), true);
	}
};
```

---

<div class="post-metadata">

**Author:** ![ZioGuido](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/zioguido/32/6975_2.png) [@ZioGuido](https://forum.juce.com/u/ZioGuido)\
**Post date:** [February 10, 2020, 3:41pm UTC](https://forum.juce.com/t/is-there-a-easy-way-to-replace-alertwindow-runmodalloop/15072/18 "2020-02-10T15:41:22Z")

</div>

Now this is a separate yet related issue: the code above shows the dialog window but I can’t get the touch keyboard to show up. Apparently, the text field does not gain the keyboard focus. What to do in this case?
