WebView is a view that displays web pages inside your application. You can also specify HTML strings and can show them inside your application using WebView. WebView makes turns your application into a web application. (Creating webview through Android Studio)
Contents
WebView Tutorial With Example In Android Studio
In Android, WebView is a view used to display the web pages in an application. This class is the basis upon which you can roll your own web browser or simply use it to display some online content within your Activity. We can also specify HTML strings and can show them inside our application using a WebView. Basically, WebView turns an application into a web application.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="myApp2.com.example.myapplication2.myapp2"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="myApp2.com.example.myapplication2.myapp2.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="myApp2.com.example.myapplication2.myapp2.MainActivity"> <WebView android:id="@+id/activity_main_webview" android:layout_width="match_parent" android:layout_height="match_parent" />
MainActivity.java
package myApp2.com.example.myapplication2.myapp2; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebSettings; import android.webkit.WebView; public class MainActivity extends ActionBarActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { mWebView = (WebView) findViewById(R.id.activity_main_webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView.loadUrl("http://beta.html5test.com/"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Methods defined in WebView class
Sr.No | Method & Description |
---|---|
1 | canGoBack() This method specifies the WebView has a back history item. |
2 | canGoForward() This method specifies the WebView has a forward history item. |
3 | clearHistory() This method will clear the WebView forward and backward history. |
4 | destroy() This method destroys the internal state of WebView. |
5 | findAllAsync(String find) This method finds all instances of string and highlights them. |
6 | getProgress() This method gets the progress of the current page. |
7 | getTitle() This method returns the title of the current page. |
8 | getUrl() This method returns the URL of the current page. |
loadUrl() – Load a web page in our WebView
loadUrl(String url)This function is used to load a web page in a web view of our application. In this method, we specify the URL of the web page that should be loaded in a web view. (Creating webview through Android Studio)