Home » Android » Android : Html Anchor Link works only once in webview

Android : Html Anchor Link works only once in webview

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am facing some strange issue in webview at time of loading html content with anchor link.
Following code works perfect for anchor tag but only once.
Second time when i press anchor tag it is not working.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mywebview);

        String html = "<html><body><p><a href='#C4'>See also Chapter 4</a></p><p><h2>Chapter 1</h2><p>This chapter explains ba bla bla</p><h2>Chapter 2</h2><p>This chapter explains ba bla bla</p><h2>Chapter 3</h2><p>This chapter explains ba bla bla</p><h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p><h2>Chapter 5</h2><p>This chapter explains ba bla bla</p><h2>Chapter 6</h2><p>This chapter explains ba bla bla</p><h2>Chapter 7</h2><p>This chapter explains ba bla bla</p><h2>Chapter 8</h2><p>This chapter explains ba bla bla</p><h2>Chapter 9</h2><p>This chapter explains ba bla bla</p></body></html>";
        String mime = "text/html";
        String encoding = "utf-8";

        WebView myWebView = (WebView)this.findViewById(R.id.webView1);
        myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

    }

i have tried this link but not worked for me.

UPDATE:

Testing result of my code in various device.

Working
1. Samsung S Plus –> 2.3.4
2. Samsung Galaxy Y –> 2.3.6
3. Samsung Galaxy Tab –> 2.3.3
4. Motorola Xoom –> 3.2

Not Working
1. LG Optimus –> 2.2
2. DELL XCD35(ZTE Blade) –> 2.2
3. HTC WildFire –> 2.2.1

Is this OS issue or something else? Any Solution for this??


How to&Answers:

The problem was to Reload the Page again after Anchor link Click.

I have used the following code,

chapters.xml in Assets folder

<html>
<body>
<p><a href="#C4">See also Chapter 4</a></p>
<p><h2><a name='C1'>Chapter 1<a></h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 2</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 3</h2><p>This chapter explains ba bla bla</p>
<h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 5</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 6</h2><p>This chapter explains ba bla bla</p>
<h2>Chapter 7</h2><p>This chapter explains ba bla bla</p>
<a href="#C1">See also Chapter 1</a>
</body>
</html>

JAVA code : First Way

public class MainActivity extends Activity {
    WebView myWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myWebView = new WebView(this);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_asset/chapters.html");

        setContentView(myWebView);

        final GestureDetector gestureDetector = new GestureDetector(
                new MyGestureDetector());
        View.OnTouchListener gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        myWebView.setOnTouchListener(gestureListener);
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            myWebView.reload();
            Log.i("", "Reload");
            return super.onSingleTapConfirmed(e);

        }
    }
}

EDIT JAVA CODE : Second Way – I have tried this thing insted onTouchListener and that working fine for me.

public class MainActivity extends Activity {
    WebView myWebView;
    public static boolean flag = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myWebView = new WebView(this);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_asset/chapters.html");
        setContentView(myWebView);
        myWebView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                if (url.contains("#") && flag == false) {
                    myWebView.loadUrl(url);
                    flag = true;
                } else {
                    flag = false;
                }
            }

        });
    }
}

To Open Another HTML File’s Anchor Tag from One file

        myWebView = new WebView(this);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("file:///android_asset/1.htm");
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try{
                chapter = url.substring(url.indexOf("#"),url.length());
                url = url.substring(0,url.indexOf("#"));
                }catch (Exception e) {
                    chapter = "";
                }
                myWebView.loadUrl(url);
                return true;
            }
            public void onPageFinished(WebView view, String url) {
                if (!chapter.equalsIgnoreCase("") && flag == false) {
                    myWebView.loadUrl(url+chapter);
                    flag = true;
                } else {
                    flag = false;
                }
            }
        });
        setContentView(myWebView);

Answer:

Please try with this code . It works fine .

   String html = "<html><body><p><a href='#C4'>See also Chapter 4</a></p><p><h2>Chapter 1</h2><p>This chapter explains ba bla bla</p><h2>Chapter 2</h2><p>This chapter explains ba bla bla</p><h2>Chapter 3</h2><p>This chapter explains ba bla bla</p><h2><a name='C4'>Chapter 4</a></h2><p>This chapter explains ba bla bla</p><h2>Chapter 5</h2><p>This chapter explains ba bla bla</p><h2>Chapter 6</h2><p>This chapter explains ba bla bla</p><h2>Chapter 7</h2><p>This chapter explains ba bla bla</p><h2>Chapter 8</h2><p>This chapter explains ba bla bla</p><h2>Chapter 9</h2><p>This chapter explains ba bla bla</p></body></html>";
    String mime = "text/html";
    String encoding = "utf-8";

    final WebView myWebView = (WebView)this.findViewById(R.id.webView);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadDataWithBaseURL("app:html", html, mime, encoding, null);

    myWebView.setWebViewClient(new WebViewClient() 
    {  
       @Override  
       public void onPageFinished(WebView view, String url) 
       {
           super.onPageFinished(view, "app:html");
           view.loadDataWithBaseURL("app:html", html, mime, encoding, null);
       }  
    });

Answer:

This problem happens on Android 4.0.3 and 4.0.4 devices: If you have a mobile web page (either in assets or loaded from the web) whose contents fill across the whole screen width, then some links like this:

<a href=... HTML links

…will not work! They will just fail to load the next page! It’s a bit strange.

The bug is documented here: http://code.google.com/p/android/issues/detail?id=929

Even though it was logged in 2009, it does still happen on Android 4.0.3/4.0.4 devices.

The solution is to not use:

android:layout_height="wrap_content on the WebView.

Replace it with:

android:layout_height="match_parent

Answer:

Try this simple 2-liner javascript

<html>
<body>
<head>
<script language="javascript">
      function jump_top() {
          location.href = "#top";
          location.hash = "";
      }
</script>
</head>

<body>
<a name="top"></a>
<br /><br /><br /><br />
...
<br />Now you are at the bottom of the page
<br />
<a onclick="jump_top()">To Top</a>
</body>
</html>