Doesn't webview support a feature you, or your team wants? it Doesn't matter anymore, we will support it together.
Since we are busy developers I will not waste your time.
If you want to do any of the following:
Send data to some web app from Android.
Receive data from some web app from Android.
Then this article is for you, keep reading, else you can read it too, it won't hurt.
Sending Data to Web App
Let's say I've some web app I need to pass some data to it, for example:
Sending string or JSON object
Okay, so let's begin
Creating a webview
I have the following XML layout
Also, don't forget to add the INTERNET permission
Let's run the app
Now we are ready, let's begin code injection
And of course, you can predict the DOM
Let's try it
Now we are done. Let's go deeper with the next step, receiving data from a web app.
Receiving Data from Web App
Let's say I've some web app that needs to deliver some data to the android side, such as the following:
Sending a string
Triggering some native code
Navigating to another screen from the android side
To implement such features we need to do the following
Now Let's say I want to display a native toast message from the web app
You may read the last paragraph again, I know it's difficult to wrap your head around it.
Now, when shall we call this method?
Binding the interface
Like any other android view setup, we need it to be done after the view creation. So we will bind the interface in the onCreate in activities or the onViewCreated in fragments.
And the bind function attaches the interface on the key JsMediator
The final step
So we will create a CustomWebViewClient
This code will inject the mediator after the page is started loading.
The final setup code for the web view will be
Now we are not done yet.
We are all good Android Citizens, we all should clear resources after usage, this part is easy, just bind the interface and unbind it in proper view lifecycle methods.
Also, I applied some of the clean code principles to the code. All of that and more, I will leave this part for you to see for yourself.
And that's the moment, I have to end this article at. All the code I used for this tutorial is hosted at my GitHub account 🤗🤗
in this article, I talked about how we can use web views for our benefit and how to interact with them.