Show progress Dialog while loading webview in android and hide progress Dialog automatically after webview page finish loading.
FULL VIDEO HERE:-
Their are mainly 3 files use in this project:-
1. src/WebActivity.java :-WebActivity class that Show progress bar while loading webview in android and hide progress bar automatically after webview page finish loading
2.res/layout/activity_web.xml :-activity_web.xml is the layout file of WebActivity.java
3.manifests/AndroidManifest.xml :-use this user permission in androidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
|
CODING START HERE->>
STEP 1. Create a new Project WebviewWithProgressbar in Android Studio.
STEP 2. manifests/AndroidManifest.xml :-use this user permission in androidManifest.xml
<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="com.example.amitrawat.webviewwithprogressbar"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="ProgrammingCodeTech" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".WebActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
STEP 3. Write code in java file WebActivity.java and xml file activity_web.xml under -
1. res/layout/activity_web.xml :-activity_web.xml is the layout file of WebActivity.java
activity_web.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_web" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.amitrawat.webviewwithprogressbar.WebActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> |
1. src/WebActivity.java :-WebActivity class that Show progress bar while loading webview in android and hide progress bar automatically after webview page finish loading.
WebActivity.java
package com.example.amitrawat.webviewwithprogressbar;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebActivity extends AppCompatActivity {
private WebView wv;
private ProgressDialog pd;
String url = "http://programmingcodetech.blogspot.in";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv = (WebView) findViewById(R.id.webview);
pd = new ProgressDialog(WebActivity.this);
pd.setMessage("Loading website........");
wv.setWebViewClient(new Mybrowser(pd));
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl(url);
}
//class for opening webview
public class Mybrowser extends WebViewClient {
ProgressDialog pd;
public Mybrowser(ProgressDialog pd) {
this.pd = pd;
pd.show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pd.isShowing()) {
pd.dismiss();
}
}
@Override public void onReceivedError(WebView view,
int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(getApplicationContext(),
"Error:" + description, Toast.LENGTH_SHORT).show();
}
}
}
|
No comments:
Post a Comment