Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Html Anchor Link works only once in webview

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??


like image 351
Mihir Palkhiwala Avatar asked Jun 25 '12 11:06

Mihir Palkhiwala


2 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);
like image 162
MKJParekh Avatar answered Nov 05 '22 17:11

MKJParekh


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);
       }  
    });
like image 43
Chirag Avatar answered Nov 05 '22 18:11

Chirag