Inline WebView Within Your kivy App


When Developing an application, you might often need the user to visit
a webview temporary and then return to your application.
With python, you can simply use the 'webrowser' module to open up an external browser on android

Example:
This will open up an external browser, else if there are multiple browser installed,
it will open a chooser for the user to select one.
But most times, for a better user experience we might not want the user to leave
our application and still access the web.
Then we might want to make use of an Embedded WebView

But wait a minute you might be wondering that your have seen something like this
before and how to use it on android on kivy wiki

But it doesn't work,
Why, taking a look at the code
what it basically does is
1). It create a webview
2). Sets a webview client for the webview
3). And loads the webview


Ok everything above works well, but the problem arises when you want to go back
to the main application. usually you would want to watch for the on_key_down button
from the window to detect when the back button is pressed so you can go back within
the webview and then when you can't go back anymore you close the webview

But when calling setContentView() on the webview android looses our
view and puts the app on pause which also put the python interpreter on pause
which means you won't recieve any callbacks

even when the code above is written in java and you get a back button callback
setting setContentView() and removeView() isn't stable and might crash your application

so what do we do
Instead of of using setContentView(), we get our activity main layout


with this our application won't pause and we get our normal callbacks.
to remove the webview you just simple say

# link to file
The whole code is in this file below ( android_inline.py ),
so basically you can import the code and it will register the android inline browser
on the webbrowser,
so finally in your code you can just run


Comments

  1. Thank you for your help,
    What function do I have to use to quit my webview and allow to continue the python script?

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between OFood and OrderEat