I'm going to preface this by saying I am an absolute noob at coding anything in Android (I'm basically learning how to do everything through you awesome guys and gals at Stack Overflow :) ). I am making an app that is completely just a WebView component and I want it to detect when a link in that WebView page has been clicked, get the url of that link, and then redirect the user to a different link if the original link has a certain string in it. Finally, the WebView needs to be reloaded either with the original link or redirected link.
Here's my code for the java side of the app where I think the error is happening:
public class Grades extends AppCompatActivity {
private WebView webview;
String ShowOrHideWebViewInitialUse = "show";
private ProgressBar spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grades);
webview =(WebView)findViewById(R.id.webView);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
webview.setWebViewClient(new CustomWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("myurl.com");
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(webview.INVISIBLE);
}
}
@Override
public void onPageFinished(WebView view, String url) {
spinner.setVisibility(View.GONE);
ShowOrHideWebViewInitialUse = "hide";
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("/courses/")) {
url = url + "/grades"
WebView.loadUrl(url);
return false;
} else {
WebView.loadUrl(url)
}
}
}
}
I did some troubleshooting and added code that made the app display the url in the logcat in the last @override. The problem there was that the WebView didn't load the page, instead there was just an endless loop in the logcat of it printing the url of the current page for the WebView, not the url of the link. So to me it seems that my problem is twofold and I have no idea what to do.
For info: the middle two @overrides are just for displaying a spinner while the main page is loading when the app starts up. Everything seems to work fine until the last @override.
I figured it out. The last override should be:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println(Uri.parse(url).getHost());
if ("myurl.com".equals(Uri.parse(url).getHost())) {
webview.loadUrl(url);
String newurl = webview.getUrl();
System.out.println(newurl);
if (newurl.contains("/grades") || newurl.contains("/users") || newurl.contains("/assignments")) {
webview.loadUrl(newurl);
return false;
}
if (newurl.contains("myurl.com") && newurl.contains("/grades") == false && newurl.contains("/users") == false && newurl.contains("/assignments") == false) {
newurl = newurl + "/grades";
System.out.println(newurl);
webview.loadUrl(newurl);
return false;
}
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments