This is the code I got from internet which allow to use webview in kivy and it works perfectly well:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
@run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com')
class ServiceApp(App):
def build(self):
return Wv()
if __name__ == '__main__':
ServiceApp().run() However I am unable to use it as a widget. I want to design a carousel with two screens, one with webview and the other with one of my own widget.
I wrote the below code but it just renders the webview and I am unable to slide between the two screens.
main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, ListProperty, NumericProperty, StringProperty
import json
from kivy.network.urlrequest import UrlRequest
import urllib
from kivy.factory import Factory
import kivy
from kivy.lang import Builder
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
LOGGED_IN=""
class RentRoot(Screen):
carousel = ObjectProperty()
def __init__(self, **kwargs):
super(RentRoot, self).__init__(**kwargs)
class LoginPage(BoxLayout):
def login_request(self, req, result):
print result
if result['error'] == False:
self.clear_widgets()
self.add_widget(ThankYou)
global LOGGED_IN
LOGGED_IN=result['email']
def validate_login(self):
print "in validate_login"
email=self.email.text
password=self.password.text
print email, password
params = urllib.urlencode({'email':email, 'password':password})
req = UrlRequest('http://localhost:8000/api/login/', on_success=self.login_request, req_body=params)
class RentApp(App):
pass
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
@run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com')
if __name__ == "__main__":
RentApp().run()Enter code here... rent.kv:
RentRoot:
<RentRoot>:
carousel: carousel
loginpage: loginpage
wv: wv
Carousel:
id: carousel
LoginPage
id: loginpage
Wv:
id:wv
<LoginPage>:
email: email
password: password
FloatLayout:
Label:
text: 'Log In'
pos: 350, 450
size_hint: .1, .2
font_size: 36
TextInput:
#text: ""
id: email
hint_text: 'Username'
font_size: 20
pos: 280, 350
size_hint: .3, .07
TextInput:
#text: ""
id: password
hint_text: 'Password'
font_size: 20
pos: 280, 300
size_hint: .3, .07
Button:
text: 'Log In'
pos: 280, 200
size_hint: .3, .07
font_size: 16
on_press: root.validate_login()
Can someone please help me. Its urgent.