mytechead

Android & Wordpress Help center

Android: WebView, enable / disable navigation buttons

A very convenient way of changing the state of navigation buttons [forward / back button] of a webview as enabled and disabled as per the availability of next and previous pages is to add this function in your code, and call it from onPageFinished() method of your WebViewClient class.

Code snippet:

mWebView.setWebViewClient(new MyWebViewClient());

public class MyWebViewClient extends WebViewClient{
   @Override
   public void onPageFinished(WebView view, String url) {
         super.onPageFinished(view, url);

         enableDisableNavigationButtons();
   }

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

   @Override
   public void onReceivedError(WebView view, int errorCode,
     String description, String failingUrl) {
     super.onReceivedError(view, errorCode, description, failingUrl);
   }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return super.shouldOverrideUrlLoading(view, url);
    }
}

 

/**
* Changes state of navigation buttons – Disable / Enable as per
* the availability of previous / next pages.
*/
public void enableDisableNavigationButtons(WebView mWebView) {
// if has previous page, enable the back button

if(mWebView.canGoBack()){
  backButton.setImageDrawable(getResources().getDrawable(R.drawable.back));
}else{
  backButton.setImageDrawable(getResources().getDrawable(R.drawable.back_disabled));
}
// if has next page, enable the next button

if(mWebView.canGoForward()){
  forwardButton.setImageDrawable(getResources().getDrawable(R.drawable.forward));
}else{
  forwardButton.setImageDrawable(getResources().getDrawable(R.drawable.forward_disabled));
}
}

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: