75% of the Android Web Browser examples don't use an EditText to allow the user to change Urls. In that last 25% which give you a sample EditText to change the Url they stop short and don't clue you in on how to keep that Url EditText updated after the page finishes loading. There are many times when my displayed Url doesn't match the current page I was redirected to.
Problem seems to be that I'd like to put MyWebViewClient class in a Java file separate from the main activity. My custom WebViewClient allows me to use onPageFinished(WebView view, String url) override method to update my variable when the page finishes loading but I hit a snag when it happens to be the main activities edittext box.
So someone mentioned using a callback for a TextView in the main activity so I figured this might be a similar situation. I just don't know if the callback trick will work. Maybe there is a completely different way to solve the problem of keeping EditText element updated after each page load.
Here is my error:
MyWebViewClient (MyCallBack)in MyWebViewClient cannot be applied to ()
Here is my Main Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());//<---Error here
editText = (EditText) findViewById(R.id.url_search);
...
}
Here is MyWebViewClient.java Class file
package com.example.owner.webbrowserwithnavigation;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import java.net.URI;
import java.net.URISyntaxException;
public class MyWebViewClient extends WebViewClient {
MyCallBack mCallBack = null;
public MyWebViewClient(MyCallBack callBack){
this.mCallBack=callBack;
}
public void onPageFinished(WebView view, String url) {
this.mCallBack.UpdateMyText(url);
}
}
Here is MyCallback.java Class file
package com.example.owner.webbrowserwithnavigation;
interface MyCallBack
{
// Declaration of the template function for the interface
public void UpdateMyText(String mystr);
}
Maybe I'm going about it all wrong. Maybe there is a simple way to do this but the WebViewClient needs to update the EditText from the Main Activity after a page loads so it seems I need to call it from the WebViewClient.
Thanks for any tips.
Use MyWebViewClient like this:
1. Using Anonymous class
myWebView.setWebViewClient(new MyWebViewClient(new MyCallBack(){
@Override
public void UpdateMyText(String mystr) {
editText.setText(mystr);
}
}));
instead of
myWebView.setWebViewClient(new MyWebViewClient());
2. OR By implement your Callback
public class YourActivity extends AppCompatActivity implements MyCallBack{
......
......
@Override
public void UpdateMyText(String mystr) {
editText.setText(mystr);
}
}
then set webview client like:
myWebView.setWebViewClient(new MyWebViewClient(this));