WebView how to detect user selected the input box on the any webpage?

from the CommonsWare Community archives

At February 8, 2019, 12:12am, leonsaber asked:

Base on some reasons, I need to know the user clicks an input box on any webpage through WebView.

  1. Which kind of android listener I can use to detect use clicks an input box on the webpage.

  2. If the input box already has some texts, how I get them through WebView

  3. If I need to send some texts to the input box through WebView, how should I do?


At February 8, 2019, 12:26am, mmurphy replied:

None, strictly speaking.

You would need to add JavaScript to your Web page that would catch the relevant DOM events, then have that JavaScript communicate back to the hosting activity. That communication could be via a callback registered via addJavascriptInterface() or through HTML message channels. I cover both in The Busy Coder’s Guide to Android Devevelopment.

Your JavaScript would handle that.

Use evaluateJavascript() or loadUrl() with a javascript: URL. I cover both of those in The Busy Coder’s Guide to Android Devevelopment as well.

The four Sensor* projects illustrate various techniques for communicating between an activity and JavaScript in a WebView.


At February 8, 2019, 12:38am, leonsaber replied:

Thank you so much for your help, I already reach to addJavascriptInterface(), I am wondering how Android Browser apps detect all user clicks input box event. Or on the other hand, all these browsers did the same thing through addJavascriptInterface()?


At February 8, 2019, 12:53am, mmurphy replied:

I do not know that they do detect it. I would imagine that it is handled in the Web engine that the browser uses.

I would imagine that major browsers use some other Web rendering engine. The Firefox family of browsers uses a version of Gecko that works on Android, for example. I would expect others to include their own builds of Chromium via the NDK.

Even for those that use WebView, there may not be a reason for them to find out about when the user focuses on an input box in their Java code.


At February 8, 2019, 3:25am, leonsaber replied:

I through Android Presentation create back-end service and put WebView on external extend display because on the extend display WebView cannot get focus, so I can not open the soft-keyboard and make the input that is the reason I am seeking some solution, I want to make a kind of app which can support visit a website on extend display and doing other things on the phone. Input is a big problem in this case :frowning:


At February 13, 2019, 9:58pm, leonsaber replied:

Hi mmurphy:

Base on your WebView Sensor project, I have successfully set onfocus() attribute for all INPUT and TEXTAREA in the webpage, when user click input or textarea I can through an EditView get and set text and use soft-keyboard in cellphone screen. But I found, this solution not 100% cover all webpage, some time is not working, for example, Office 365 login page, I can set text in account name and password, but I will get warning “account name is not valid” when I click login, in effect all input is correct. Do you have any other good suggestions? I steel seeking a more powerful solution. Thank you so much.


At February 14, 2019, 12:21am, mmurphy replied:

You could look at other Web rendering engines, such as GeckoView, to see if they give you more options.

Other than that, sorry, I am out of ideas.


At February 14, 2019, 11:03pm, leonsaber replied:

Thank you so much mmurphy, I will still tying.

One more question, how to get Presentation Dialog through WindowManager?


At February 15, 2019, 12:05am, mmurphy replied:

If you mean "how do I get an instance of PresentationDialog from WindowManager", I don’t think that’s possible.


At February 15, 2019, 12:07am, leonsaber replied:

:sweat_smile: All right thank you so much