java - android web browser does not open url -
i want open url through browser when click on button not open url please tell me made mistake. here code of java file.
public class mainactivity extends appcompatactivity { button button; edittext edittext; webview webview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button)findviewbyid(r.id.but); edittext = (edittext)findviewbyid(r.id.edittext); webview = (webview)findviewbyid(r.id.webview); } public void goo(view view){ string url = edittext.gettext().tostring(); webview.getsettings().setloadsimagesautomatically(true); webview.getsettings().setjavascriptenabled(true); webview.loadurl(url); } }
here xml file
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="enter url" android:id="@+id/textview" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edittext"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="goo" android:text="go" android:id="@+id/but" /> <webview android:layout_width="fill_parent" android:layout_height="match_parent" android:id="@+id/webview" /> </linearlayout>
in activity copy paste following code...
public class mainactivity extends appcompatactivity { button button1, button2; edittext edittext; webview webview; string url; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button1 = (button)findviewbyid(r.id.but); button2 = (button)findviewbyid(r.id.but1); edittext = (edittext)findviewbyid(r.id.edittext); webview = (webview)findviewbyid(r.id.webview); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { url=edittext.gettext().tostring(); url="http://"+url; loadurl(url); } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { url="http://www.stackoverflow.com"; loadurl(url); } }); } public void loadurl(string url) { webview.getsettings().setjavascriptenabled(true); webview.getsettings().setbuiltinzoomcontrols(true); webview.getsettings().setusewideviewport(true); webview.getsettings().setloadwithoverviewmode(true); webview.getsettings().setdomstorageenabled(true); webview.setvisibility(view.visible); webview.loadurl(url); webview.setwebviewclient(new webviewclient() { @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return true; } }); } }
and in manifest file.. add these 2 lines..
<uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet" />
in xml, added,
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="go" android:id="@+id/but" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fb" android:id="@+id/but1" />
Comments
Post a Comment