How can I get my app to get the URL of a link clicked in WebView, perform operations on it, and then load it?

Tanay Neotia :

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.

Tanay Neotia :

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can i get clicked link href?

How can I make my webview load a url shared from another app?

How can I get the name of a clicked cell in my listview to load into a new activity?

How can I get a portion of my C# app to load dynamically without app restart?

How can I get my SSDs write speeds to perform normally?

webview, how can I find out which link was clicked in it?

How can I get the URL from webView in swift

How can I get my appended link text on click?

How can I get my anchor link to smoothly scroll down?

I can't seem to get my fragment to load webview without crashing

How can I get a signed Java Applet to perform privileged operations when called from unsigned Javascript?

How can I get this App Store purchase window in my app?

How can I get my firebase listener to load data to my redux state in a React Native app so I can read the data within my ComponentDidMount function?

How can I save a URL and load this URL in webview (android studio)

How can I get an empty link "clicked" programmatically in React.js?

How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

How can i get webview to remember cookies?

How can I make a search button that can combine my site url + the text on the input, and when the button was clicked, it will go to that link?

How can I get prometheus exporters running in my grails app?

How can I get information about the file that launched my app?

How can I get the font names bundled in my iOS app?

How can I get my app screen time on a hourly basis?

How can I get the number of online users now in my app

on Heroku how can I get the current release number of my app

How can I get my iOS app connected with api?

how can i get json object data in my url in vuejs

How can I get {% url logout %} to work in my Django template?

How can I get the URL of my image in Picasso

How can i get my url parameter right on Javascript?