Android Webview Example

Android Webview Example

Android Webview is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using Webview. Webview makes turns your application into a web application.

To create Webview first, you need to create Webview in activity XML.


Related Post: Set typeface for webview in android

Related Post: WebView with loader in android example


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>

Map the Webview into the Activity java File.

and enable the setJavaScriptEnabled(true)

then load the URL into the Webview.

webview.loadUrl(“”);

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new MyBrowser());
webView.loadUrl("https://velmuruganandroidcoding.blogspot.in");

Methods of Webview Class.

canGoBack() – This method specifies the WebView has a back history item.
canGoForward() – This method specifies the WebView has a forward history item.
clearHistory() – This method will clear the WebView forward and backward history.

destroy() – This method destroys the internal state of WebView.

findAllAsync(String find) – This method finds all instances of string and highlights them.

getProgress() – This method gets the progress of the current page.

getTitle() – This method returns the title of the current page.

getUrl() – This method returns the URL of the current page.

If you click on any link inside the webpage of the WebView, That page will not be loaded inside your WebView. In order to do that you need to extend your class from WebViewClient and override its method.

private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

Android ConstraintLayout Example

Create Reusable Alert Dialog in Android


Example :

MainActivity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView webView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.setWebViewClient(new MyBrowser());
        webView.loadUrl("https://velmuruganandroidcoding.blogspot.in");
    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="rajaapps.lifeandtechnology.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>

Don’t forget to add internet permission

<uses-permission android:name="android.permission.INTERNET"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="rajaapps.lifeandtechnology">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

 

Screenshot:

webview in android studio
Webview in android studio

Download

Leave a Reply

Your email address will not be published. Required fields are marked *