koushik
koushik

Reputation: 364

close webview in kivy

iam trying to close or destroy the webview and show other layout here is my code for webview or if that won't possible i want to get the link of the current page and if current page link is equal to some link i want to exit the webview pls help me out this

from android.runnable import run_on_ui_thread as run_thread
from jnius import autoclass
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.clock import Clock, mainthread
@mainthread
def quit_screen():
    app = App.get_running_app()
    e=GridLayout()
    d1=button(text="hi end")
    e.add_widget(e)
    app.root.switch_screen(e)
@run_thread
def key_back_handler():
    webview.loadUrl("about:blank")
    webview.clearHistory()
    webview.clearCache(True)
    webview.clearFormData()
    webview.freeMemory()
    Clock.schedule_once(quit_screen, 0)
@run_thread
def WebView(link,*args):
    WebV = autoclass('android.webkit.WebView')
    WebViewClient = autoclass('android.webkit.WebViewClient')
    activity = autoclass('org.kivy.android.PythonActivity').mActivity

    webview = WebV(activity)
    settings = webview.getSettings()
    settings.setJavaScriptEnabled(True)
    settings.setUseWideViewPort(True) 
    settings.setLoadWithOverviewMode(True) 
    settings.setSupportZoom(True)
    settings.setBuiltInZoomControls(True)
    wvc = WebViewClient()
    webview.setWebViewClient(wvc)
    activity.setContentView(webview)
    webview.loadUrl(link)

m=GridLayout(cols=1,rows=1)
d=GridLayout(cols=1,rows=1)
m.add_widget(d)
def Push(butoon):
    WebView("https://www.google.com")
b=Button(text="hi")
b.bind(on_press=Push)
d.add_widget(b)


runTouchApp(m).run()

like this but its not closing on back pressed on android like this image

Thanks for help in advance

Upvotes: 1

Views: 644

Answers (1)

Jack Threadfin
Jack Threadfin

Reputation: 126

The trick is to put the Webview in a Kivy ModalView and catch the back button/gesture from Java to exit the ModalView , for example:

https://github.com/RobertFlatt/Android-for-Python/tree/main/webview

Upvotes: 1

Related Questions