Use a Webview in Kivy
Fleetingfrom kivy.app import App
from kivy.uix.widget import Widget
from kivy.utils import platform
from jnius import autoclass,JavaClass, java_method, MetaJavaClass
from android.runnable import run_on_ui_thread
import time
ChromeClient = autoclass("android.webkit.WebChromeClient")
from jnius import JavaClass, MetaJavaClass
class MyChromeClient(JavaClass):
__javaclass__ = 'android/webkit/WebChromeClient'
__metaclass__ = MetaJavaClass
class AndroidWebView(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.create_webview()
def create_webview(self):
# Get the required Java classes
PythonActivity = autoclass("org.kivy.android.PythonActivity")
WebView = autoclass("android.webkit.WebView")
WebViewClient = autoclass("android.webkit.WebViewClient")
LayoutParams = autoclass("android.view.ViewGroup$LayoutParams")
# Get the current activity
activity = PythonActivity.mActivity
@run_on_ui_thread
def f():
self.webview = WebView(activity)
self.webview.getSettings().setJavaScriptEnabled(True)
self.webview.setWebViewClient(WebViewClient())
# self.webview.setWebChromeClient(ChromeClient())
# self.webview.setWebChromeClient(MyChromeClient())
self.webview.loadUrl("https://example.com") # Set the URL to load
# Add the WebView to the current activity
activity.addContentView(
self.webview, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
)
f()
time.sleep(4)
@run_on_ui_thread
def f():
print("alerting")
self.webview.evaluateJavascript("window.location = 'https://google.fr'", None)
f()
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_down(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_move(touch)
def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_up(touch)
class MyWebViewApp(App):
def build(self):
return AndroidWebView()
if __name__ == "__main__":
MyWebViewApp().run()
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.utils import platform
from jnius import autoclass,JavaClass, java_method, MetaJavaClass
from android.runnable import run_on_ui_thread
import time
ChromeClient = autoclass("android.webkit.WebChromeClient")
from jnius import JavaClass, MetaJavaClass
class MyChromeClient(JavaClass):
__javaclass__ = 'android/webkit/WebChromeClient'
__metaclass__ = MetaJavaClass
class AndroidWebView(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.create_webview()
def create_webview(self):
# Get the required Java classes
PythonActivity = autoclass("org.kivy.android.PythonActivity")
WebView = autoclass("android.webkit.WebView")
WebViewClient = autoclass("android.webkit.WebViewClient")
LayoutParams = autoclass("android.view.ViewGroup$LayoutParams")
# Get the current activity
activity = PythonActivity.mActivity
@run_on_ui_thread
def f():
self.webview = WebView(activity)
self.webview.getSettings().setJavaScriptEnabled(True)
self.webview.setWebViewClient(WebViewClient())
# self.webview.setWebChromeClient(ChromeClient())
# self.webview.setWebChromeClient(MyChromeClient())
self.webview.loadUrl("https://example.com") # Set the URL to load
# Add the WebView to the current activity
activity.addContentView(
self.webview, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
)
f()
time.sleep(4)
@run_on_ui_thread
def f():
print("alerting")
self.webview.evaluateJavascript("window.location = 'https://google.fr'", None)
f()
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_down(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_move(touch)
def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
return True
return super().on_touch_up(touch)
class MyWebViewApp(App):
def build(self):
return AndroidWebView()
if __name__ == "__main__":
MyWebViewApp().run()