Android Webview Progress Bar

Add a Progress Bar in WebView

I have added few lines in your code and now its working fine with progress bar.

        getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main );
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
setTitle("Loading...");
setProgress(progress * 100); //Make the bar disappear after URL is loaded

// Return the app name after finish loading
if(progress == 100)
setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");

How to add a progress/loading bar in WebView

Try this i have make some changes in your code

import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.support.v7.widget.Toolbar;
public class Main2Activity extends AppCompatActivity {

WebView webView;

SwipeRefreshLayout swipe;
ProgressBar progressBar;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
LoadWeb();

progressBar.setMax(100);
progressBar.setProgress(1);


swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {


progressBar.setProgress(progress);
}
});

webView.setWebViewClient(new WebViewClient() {


@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}


public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

webView.loadUrl("file:///android_asset/error.html");
}

public void onLoadResource(WebView view, String url) { //Doesn't work
//swipe.setRefreshing(true);
}

public void onPageFinished(WebView view, String url) {

//Hide the SwipeReefreshLayout
progressBar.setVisibility(View.GONE);
swipe.setRefreshing(false);
}

});


}

public void LoadWeb() {

webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("https://www.google.com/");
swipe.setRefreshing(true);
}

@Override
public void onBackPressed() {

if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}

}

XML.LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/rel_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">



<ProgressBar
android:id="@+id/awv_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_marginTop="7dp"
android:indeterminate="true" />


<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
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.v4.widget.SwipeRefreshLayout>


</LinearLayout>

add this theme to your activity

<style name="AppTheme3" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

OUTPUT

Sample Image

Progress bar for internal links of a WebView in Android Studio

Instead of a Spinner progress bar, you can use horizontal bar by changing the style.

<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
--- other attributes here ---
/>

Place this horizontal progress bar on the top of the webview similar to the image below.

Progress bar mock up

Additionally, by setting chromeWebClient to the webview and overriding onProgressChanged method we can get the progress of the page loading which can be set to the horizontal progress bar.

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgressCompat(newProgress, true)
}
});

How to display a progress bar when WebView loads a URL, in Android ?

try this code... you need webChoromeClient to track the webview load progress...

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);

} else {
progressBar.setVisibility(View.VISIBLE);

}
}
});

Replace your webview client with this code....

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

progress bar in android webview


public class About extends AppCompatActivity {

WebView myWebView;
ProgressBar progressBar;
FrameLayout frameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("About CreativeGraphy");
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}

progressBar = findViewById(R.id.progress_bar);
frameLayout = findViewById(R.id.frame_loading);
progressBar.setMax(100);

myWebView = findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.co.in/");
myWebView.setWebViewClient(new HelpClient());

myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
frameLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
setTitle("Loading....");
if (newProgress == 100) {
frameLayout.setVisibility(View.GONE);
//setTitle(View.getTitle());
}
super.onProgressChanged(view, newProgress);
}
});
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setVerticalScrollBarEnabled(false);
progressBar.setProgress(0);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}//close activity when click back button
return super.onOptionsItemSelected(item);
}

public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if (myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}

private class HelpClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
frameLayout.setVisibility(View.VISIBLE);
return true;
}
}
}

try this
it has loading

layout

    <?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="horizontal"
tools:context=".About">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/ToolbarColoredBackArrow" />
</android.support.design.widget.AppBarLayout>

<FrameLayout
android:id="@+id/frame_loading"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="@id/app_bar"
android:background="@android:color/transparent">

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:background="@android:color/transparent"
android:progress="20"
android:progressDrawable="@drawable/progress_cyclic" />
</FrameLayout>

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/frame_loading" />

</RelativeLayout>

try using 2 webview if its 100% loaded visible it other one gone first will stay while other loads when it is loaded second web view starts loading

How to hide Progress Bar in Android WebView

you can try this in your "onProgressChanged()".

if (newProgress == 100) {
superProgressBar.setVisibility(View.GONE);
} else {
superProgressBar.setVisibility(View.VISIBLE);
superProgressBar.setProgress(newProgress);
}

Android WebView Progress bar + Swipe Refresh

Your progressbar is behind the webview

<?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"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefreshlayout"
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:layout_centerInParent="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<ProgressBar
android:id="@+id/progressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout>

Android ProgessBar while loading WebView

Check the source code. Help you and solve your problem...

public class AppWebViewClients extends WebViewClient {
private ProgressBar progressBar;

public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}

I think it help you.

Thanks.



Related Topics



Leave a reply



Submit