Widget Recent Post No.

header ads

Implement WebView in Android Studio | Android Development


Step 1 MAIN.XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

    android:orientation="vertical"

    tools:context=".MainActivity">






    <ProgressBar

        android:id="@+id/pb"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        style="@style/Widget.AppCompat.ProgressBar.Horizontal"

        android:visibility="gone"/>


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/swiperefresh"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@+id/pb">


        <WebView xmlns:android="http://schemas.android.com/apk/res/android"

            android:id="@+id/webview"

            android:layout_width="match_parent"

            android:layout_height="match_parent" />


    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>



</RelativeLayout>



Step 2 MAIN.JAVA

package in.learncodewithrk.webview;


import android.graphics.Bitmap;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.ProgressBar;


import androidx.appcompat.app.AppCompatActivity;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;



public class MainActivity extends AppCompatActivity {



    private WebView mWebView;

    private ProgressBar mProgressBar;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);





        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.getSettings().setJavaScriptEnabled(true);


        //learncodewithrk

        mWebView.loadUrl("https://www.youtube.com/channel/UCQ188dyxCgqYdG1wRdbJtpw");


        //SwipeRefreshLayout

        final SwipeRefreshLayout finalMySwipeRefreshLayout1;

        finalMySwipeRefreshLayout1 = findViewById(R.id.swiperefresh);

        finalMySwipeRefreshLayout1.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {

            @Override

            public void onRefresh() {

                // This method performs the actual data-refresh operation.

                // The method calls setRefreshing(false) when it's finished.

                mWebView.loadUrl(mWebView.getUrl());

            }

        });


        // Get the widgets reference from XML layout

        mProgressBar = findViewById(R.id.pb);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override

            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                // Visible the progressbar

                mProgressBar.setVisibility(View.VISIBLE);

            }

            @Override

            public void onPageFinished(WebView view, String url) {

                finalMySwipeRefreshLayout1.setRefreshing(false);

                mProgressBar.setVisibility(View.GONE);

            }

        });


        mWebView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int newProgress){

                // Update the progress bar with page loading progress

                mProgressBar.setProgress(newProgress);

                if(newProgress == 100){

                    // Hide the progressbar

                    mProgressBar.setVisibility(View.GONE);

                }

            }

        });





    }

}


GITHUB : https://github.com/LearncodeWithRk/WebView


 

Post a Comment

0 Comments