buffermebufferthat
buffermebufferthat

Reputation: 3

Is it possible to do something when certain url loads?

I'm coding my first android "app", and kind of cheating, since I'm using a WebView to display what would be the main "application".

I found that the app is generally faster by rendering the backgrounds as ImageViews rather than loading them from the HTML code. I have a ImageView that acts as the app's background and loads from the beginning, and is 'behind' the WebView(the WebView background is transparent).

But once the user presses enter on a text field, and is sent to another web page within the WebView, I'd like to get rid of the first background ImageView and load a new one, so that the previous one doesn't slow it down. So I guess I'd have to detect some sort of load event, and do something with that. However, I've been looking in the documentation, and I've found nothing. I don't even know where to start. Is there any way to do this? Thanks.

Here is my code, if it is of any use (but I don't think it's really relevant).

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.setBackgroundColor(0x00000000);
        mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);


        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if((mWifi != null && mWifi.isConnected()) || (mMobile != null && mMobile.isConnected())) {

            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    findViewById(R.id.splash).setVisibility(View.GONE);
                    findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
                }
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    mWebView.loadUrl("file:///android_asset/error.html");
                }
            });
            mWebView.loadUrl(got_url);
        }
        else
        {
            setContentView(R.layout.activity_main);
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    findViewById(R.id.splash).setVisibility(View.GONE);
                    findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
                }
            });
            mWebView.loadUrl("file:///android_asset/error-nointernet.html");
        }

    }

What I ended up doing, thanks to Zafar:

        public void onPageStarted(WebView view, String url) {
            if(url.equals(certainUrl)) //url returns last requested URL.
            {
                findViewById(R.id.background0).setVisibility(View.GONE);
                findViewById(R.id.background1).setVisibility(View.VISIBLE);
            }
        }

Upvotes: 0

Views: 53

Answers (1)

droid
droid

Reputation: 414

What I have got is that you want to load the background image once the webview starts loading. If it is the situation , you can use

onPageStarted

and set the image here. Check this link if you can get some more solutions.If the requirement is anything else, please do reply.

Though you have done what you wanted, I would just like to add this method, if it helps you in other situations like this.

webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("abcd://")) {
        // your code
        return true;
    }
    return false;
}
});

Upvotes: 1

Related Questions