{"id":2558,"date":"2017-05-08T18:51:17","date_gmt":"2017-05-08T18:51:17","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2558"},"modified":"2017-05-08T18:51:17","modified_gmt":"2017-05-08T18:51:17","slug":"how-to-build-web-apps-using-webview-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-build-web-apps-using-webview-in-android\/","title":{"rendered":"How to Build Web Apps using WebView in Android"},"content":{"rendered":"<p>In this tutorial how to build web apps using WebView Control in Android Application is shown.<\/p>\n<p>The WebView class is an extension of Android View class that allow you to display webpages as a part of your activity layout, it does not include any features of full developed web browser such as navigation bar, address bar etc.<\/p>\n<p>Another scenario in which WebView can help is if your app provides data to the user that always requires an internet connection to retrieve data such as image.<\/p>\n<p>To handle the navigation for webpage call goBack() method and goForward() method which will show you the history of webpages.<\/p>\n<p>The canGoBack() returns true if there is actually webpage history for the user to visit.<\/p>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;WebView<br \/>\nandroid:id=&#8221;@+id\/webView1&#8243;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>ActivityMain.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.view.Menu;<br \/>\nimport android.webkit.WebSettings;<br \/>\nimport android.webkit.WebView;<\/p>\n<p>public class MainActivity extends Activity{<\/p>\n<p>WebView wb;<br \/>\nString url;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nwb=(WebView)findViewById(R.id.webView1);<br \/>\nurl=&#8221;http:\/\/webartdevelopers.com&#8221;;<br \/>\nwb.loadUrl(url);<br \/>\nWebSettings ws=wb.getSettings();<br \/>\nws.setJavaScriptEnabled(true);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<br \/>\n}<\/p>\n<p>AndroidManifest.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;manifest xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\npackage=&#8221;com.example.demo&#8221;<br \/>\nandroid:versionCode=&#8221;1&#8243;<br \/>\nandroid:versionName=&#8221;1.0&#8243; &gt;<\/p>\n<p>&lt;uses-sdk<br \/>\nandroid:minSdkVersion=&#8221;8&#8243;<br \/>\nandroid:targetSdkVersion=&#8221;17&#8243; \/&gt;<br \/>\n&lt;uses-permission android:name=&#8221;android.permission.INTERNET&#8221;\/&gt;<br \/>\n&lt;application<br \/>\nandroid:allowBackup=&#8221;true&#8221;<br \/>\nandroid:icon=&#8221;@drawable\/ic_launcher&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221;<br \/>\nandroid:theme=&#8221;@style\/AppTheme&#8221; &gt;<br \/>\n&lt;activity<br \/>\nandroid:name=&#8221;com.example.demo.MainActivity&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221; &gt;<br \/>\n&lt;intent-filter&gt;<br \/>\n&lt;action android:name=&#8221;android.intent.action.MAIN&#8221; \/&gt;<\/p>\n<p>&lt;category android:name=&#8221;android.intent.category.LAUNCHER&#8221; \/&gt;<br \/>\n&lt;\/intent-filter&gt;<br \/>\n&lt;\/activity&gt;<br \/>\n&lt;\/application&gt;<\/p>\n<p>&lt;\/manifest&gt;<\/p>\n<p><a href=\"https:\/\/youtu.be\/Mllue2BI3UI\">https:\/\/youtu.be\/Mllue2BI3UI<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to build web apps using WebView Control in Android Application is shown. The WebView class is an extension of Android View class that allow you to display webpages as a part of your activity layout, it does not include any features of full developed web browser such as navigation bar, address bar etc. Another scenario in which WebView can help is if your app provides data to the user that always requires an internet connection to retrieve data such as image. To handle the navigation for webpage call goBack() method and goForward() method which will show you the history of webpages. The canGoBack() returns true if there is actually webpage history for the user to visit. <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[120],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2558"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2558"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2558\/revisions"}],"predecessor-version":[{"id":2560,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2558\/revisions\/2560"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}