WebView - only show back button if back history exists - java

I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.
The button is initially hidden and should only show if a back history exists.
How can I realise this?
The code is simple:
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
but where do I have to call this? How is the "some site loaded" event called?
Update:
I tried to apply #Murats answer, but nothing happens:
private WebView blizzView; // my webview
private Button backButton;
public void onPageFinished(WebView view, String url)
{
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}

You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished

It works like this:
blizzView.setWebViewClient(new WebViewClient() { //
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
});

Related

programmatically click on a HTML button in WebView from fragment in Android

I need to open a dialog in a website which opens on clicking a HTML button of the website. I'm trying to click on a HTML button based on the id from the fragment, how can I perform the action, I'm using
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// trying to click the button with id btn-to-be-clicked
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
}
}
thanks
I tried
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
so apparently you must specify every line in a new string
extracted the js to a variable with multi line, it has to be a multi line concatenated string
val jsFunction = "javascript:(function() { " +
"document.getElementById('btn-to-be-clicked').click();" +
"})()"

I want to show a button when android webview reach bottom

I have added a webview inside a dialog. I want to enable the dialog positive button when the user has scrolled the webview to the bottom, so that I can assume that the user has read the user agreement.
You can solve it via Javascript. In the webpage, add the JS code to detect if the user has scrolled to the bottom like this (credits to this post )
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
Android.onUserScrolledToBottom()
}
});
Then in your Android app, create a Javascript Interface class
class MyJavascriptInterface {
TermsAndCondActivity termsAndCondActivity;
MyJavascriptInterface(TermsAndCondActivity termsAndCondActivity) {
this.termsAndCondActivity = termsAndCondActivity;
}
#JavascriptInterface
public void onUserScrolledToBottom() {
termsAndCondActivity.onUserScrolledToBottom();
}
}
Finally in your TermsAndCondActivity, setup the WebView in the onCreate like this
myWebView = view.findViewById(R.id.my_web_view);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
And add the following method
void onUserScrolledToBottom() {
button.setEnabled(true);
}
As an example I've used an activity since you didn't specify the dialog's code, but you should be able to adapt the code easily.
Thanks for your answers. I have achieved this using onScrollChange() method.
webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
if(i1>=webView.getContentHeight()){
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
}else {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(GONE);
}
}
});

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

Invoke click through code in Webview

my name is Dan and I am trying to invoke a click on a HTML element through code in my Android application. If I try to click it through pure Javascript the result won't be the same as it is when the user clicks it.
So, how to press elements from a Webview component through Java code? Thanks in advance.
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String link) {
return super.shouldOverrideUrlLoading(view, url);
}
});

Creating An In-Game Web Browser on Android

In my game, I'd like to have a kind of "message of the day" feature. Basically, when the user taps the "message of the day" button in the main menu, it would open up a browser view in-game. When the user is done, he taps a "close" button and the view disappears, returning him to the game menu.
So, is it possible to create a browser view dynamically? If yes, how?
The following can be used in a Dialog as well
public void setWebView() {
//WebViewClient is used if you want to capture stuff from the webview, like if a link was pressed
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
open_web = true;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse(url));
startActivity(browserIntent);
return true;
}
};
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(yourWebClient);
String str = getHtml();
wv.loadData(str, "text/html", "utf-8");
wv.setBackgroundColor(0);
}
boolean isAsset = false;
private String getHtml(){
InputStream source = null;
if (isAsset){
source = getAssets().open("motd.html");
} else {
source = new FileInputStream(Environment.getExternalStorageDirectory() + "motd.html");
}
}
You could probably just use a WebView. When you want it to show up you could just set its view state to View.VISIBLE. When you want it to go away, just set its view state to View.GONE.
You can add/remove a WebView to your game dynamically (or show/hide it, whatever you prefer).
Take a look at the WebView tutorial for more info:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Make sure to leave room for your "Close" button in your layout, i.e. you don't want to set the layout height to "fill_parent".

Categories