I have an Android app with a webview that displays a form with a bunch of controls on it. Tapping on one of the controls results in a popup being displayed. Is there a way to detect if a popup is currently active in a webview?
-
As far as I know there isn't a way to detect just any popup in a `WebView`. There is a way to capture some events in a `WebView` such as when a user taps a link and a link is requested. You should consider something along the lines of this solution: http://stackoverflow.com/a/20917793/394933 – Brian Aug 18 '14 at 21:56
2 Answers
I've made 2 assumptions because the question came across slightly vague:
- case 1:popup as in dialog showing
- case 2:popup as in an ad showing
For case 1: I can think off two ways to get around this.
Via the android documentation here you can call the isShowing() method to determine if a dialog (popup) is showing, it will return true.
The other way.
Going by the limited information...have you tried setting implementing a web interface class and calling a function that will indicate whether your dialog is showing? Like this.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** inform user that a dialog is being displayed */
@JavascriptInterface
public boolean dialogIsShowing() {
// Toast.makeText(mContext, "the dialog is showing!", Toast.LENGTH_SHORT).show();
return true
}
}
...
//code embeded with html to display your dialog
<script type="text/javascript">
function showCranialsurgesDialog() {
Android.dialogIsShowing()
}
</script>
//handle the result of the dialog showing
if (dialogIsShowing()==true){
//do something
}
For case 2: popup as in ad is showing
My best guess is to use the:
'webViewSettings.setJavaScriptCanOpenWindowsAutomatically(false)' method. This stops windows from opening automatically which from theory not experience may prevent your problem. That documentation is here
Lasty this may be helpful too if case 1 is what your stuck on. CLICK ME
Hope that helps, Jonny 2 plates
-
Thanks a ton for your response. Essentially the popup I am referring to is a list of options that overlays the rest of the webpage being displayed by the webview when a button is pressed. I need this to show, but would like to know when it is showing in order to process something at this time. I'll poke around these options and see if I can put something together. – Cranialsurge Aug 19 '14 at 16:18
You can use on the mainActivity the event
public void onWindowFocusChanged (boolean hasFocus)
{
WebView view = getCurrentFocus();
String url = view.getUrl() ;
}
and check if it is the same url two time consecutive

- 21
- 5