Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic internal links don't work in honeycomb app?

Internal links do not seem to be working in Android version 3 in my published app. My app targets Froyo at this point.

The app works fine on tons of phones, but my new Galaxy Tab can't handle the internal links!! It can handle them within an html page, ie:

<a href="#faq">Go to faq</a>  <!-- goes to FAQ link -->

Goes to the tag lower on the same page:

<a name="faq" id="faq"></a>  

However from a another html file, ie the index page, the link no longer works in Honeycomb:

<a href="mainpage.html#faq">FAQ</a>  <!-- goes to error page -->

Also, if I go to an internal link, and from there follow a link to another page, then hit the back button, (it is overridden to go to previous webview page) you get the same error ie:

The webpage at file:///android_asset/folder/mainpage.html#faq might be temporarily    down or it may have moved permanently to a new web address

WTF! The webview was just on the page, but you hit back 1 second later, and it can't find it. Nor can it link from another html page, but it all works fine in 1.x, 2.x, just not 3.1 (have not tried 3.0)

NOTE: I have seen this almost identical question: android_asset not working on Honeycomb? But there are no spaces in my asset path.

I have tried with and without the webclient, and tried the DOM and cache settings to no avail. Here is an example of what I currently have in oncreate:

        browser = new WebView(this);
//  browser = (WebView) findViewById(R.id.webkit);  // tried with XML and without
    browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
//  browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//  browser.getSettings().setUseWideViewPort(true);
    browser.setBackgroundColor(Color.parseColor("#333333"));
    browser.setInitialScale(1);
    browser.getSettings().setBuiltInZoomControls(true);


    final Activity MyActivity = this;
    browser.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {
            // Make the bar disappear after URL is loaded, and changes
            // string to Loading...

            setProgressBarIndeterminateVisibility(true);

            MyActivity.setTitle("  Loading . . . " + progress + "%");
            MyActivity.setProgress(progress * 100); // Make the bar

            if (progress == 100) {
                setTitle("  APP TITLE YADA YADA");
                setProgressBarIndeterminateVisibility(false);
            }
        }
    });
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);  // note I tried with and without overriding this 
            return true;
        }

    });
    setContentView(browser);

    browser.loadUrl("file:///android_asset/folder/page.html");
like image 996
Mischa Avatar asked Jul 01 '11 01:07

Mischa


1 Answers

Here is the solution I use. Works the same on all Android platform (tested on all 1.6 to 4.0.1)

Load the file as you would normally, without the anchor point. E.g.:

webview.loadUrl("file:///android_asset/file.html");

Load the URL without the anchor point (e.g. file:///android_asset/file.html), and add:

webview.setWebViewClient(new WebViewClient()
{
    public void onPageFinished(WebView view, String url)
    {
        if (this.anchor != null)
        {
            view.loadUrl("javascript:window.location.hash='" + this.anchor + "'");
            this.anchor = null;
        }
    }
});

where anchor is the anchor point you want to jump to.

you have to make sure javascript is enabled:

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);

This is for loading the file into the webview. Now if you want to catch internal links that are now broken, as suggested in the other answer, override the onReceivedError method and insert do something like:

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    {
        int found = failingUrl.indexOf('#');

        if (found > 0)
        {
            anchor = failingUrl.substring(found + 1, failingUrl.length());
            view.loadUrl(failingUrl.substring(0, found));
        }
    }
like image 84
jyavenard Avatar answered Nov 16 '22 02:11

jyavenard