Mohini Monga Mohini Monga - 1 month ago 16
Android Question

set loader on webview

i want to set progress dialog on webview activity,below is my code , i dont know exactly where i should use loader.

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apnikheti);
WebView webView = (WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

webView.loadUrl("http://www.mywebsite.com");

}

public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}}

Answer

For Horizontal progress bar:

In your xml file, do the following changes

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/urlContainer" />

In your WebViewActivity, do the following changes

webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebViewClient());

progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);

private class MyWebViewClient extends WebChromeClient { 
    @Override
    public void onProgressChanged(WebView view, int newProgress) {          
        WebViewActivity.this.setValue(newProgress);
        super.onProgressChanged(view, newProgress);
    }
}

public void setValue(int progress) {
    this.progress.setProgress(progress);        
}

For progress dialog:

In your layout xml file, do the following changes

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:layout_below="@id/urlContainer" />

In your WebViewActivity, do the following changes

webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());

progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);

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

     @Override
    public void onPageFinished(WebView view, String url) {
         progress.setVisibility(View.GONE);             
        super.onPageFinished(view, url);
    }

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

Check here for more details ProgressBar while Loading WebView in Android

Update:

Checkout here to open link in WebView itself instead of browser