Last active
August 23, 2018 11:59
-
-
Save sandeepyohans/c5458ea50687dc3b6c91ef0ec22cf6c9 to your computer and use it in GitHub Desktop.
Add back and forward button to WebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
// This line positions the layout below AppBar | |
app:layout_behavior="@string/appbar_scrolling_view_behavior"> | |
<WebView | |
android:id="@+id/webview_devotions" | |
app:layout_behavior="@string/appbar_scrolling_view_behavior" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<Button | |
android:id="@+id/bt_back_devo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentBottom="true" | |
android:enabled="false" | |
android:text="@string/back" /> | |
<Button | |
android:id="@+id/bt_forward_devo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_alignParentEnd="true" | |
android:text="@string/forward" | |
android:enabled="false" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webView = findViewById(R.id.webview_devotions); | |
//Button Initialization | |
backButton =(Button) findViewById(R.id.bt_back); | |
forwardButton =(Button) findViewById(R.id.bt_forward); | |
//Back Button Action | |
backButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Going back if canGoBack true | |
if(webView.canGoBack()){ | |
webView.goBack(); | |
} | |
} | |
}); | |
//Forward Button Action | |
forwardButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Go Forward if canGoForward is true | |
if(webView.canGoForward()){ | |
webView.goForward(); | |
} | |
} | |
}); | |
webView.setWebViewClient(new WebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
if (document.hasAttr("footer")) { | |
document.getElementById("footer").remove(); | |
} else if (document.hasClass("header")) { | |
document.getElementsByClass("header header-default").remove(); | |
} | |
return false; | |
} | |
/** | |
* Notify the host application that the WebView will load the resource | |
* specified by the given url. | |
* | |
* @param view The WebView that is initiating the callback. | |
* @param url The url of the resource the WebView will load. | |
*/ | |
@Override | |
public void onLoadResource(WebView view, String url) { | |
Log.d(TAG, "onLoadResource entered"); | |
} | |
@Override | |
public void onPageFinished(WebView mWebView, String url) { | |
Log.d(TAG, "onPagFinished entered"); | |
//Make Enable or Disable buttons | |
backButton.setEnabled(mWebView.canGoBack()); | |
forwardButton.setEnabled(mWebView.canGoForward()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment