0

I have a view pager, each page have a textview, I have parsed html document (without javascript) in my textview, I have many anchor tags inside this document, I want to call JavaScript function in anchor tag click. For e.g.

<a class="pginternal" tag="{http://www.w3.org/1999/xhtml}a" onclick="GoToPageNumber(186)" style="color:Blue !important;cursor:pointer !important;text-decoration:underline !important">THE VAMPIRE'S SIXTH STORY — In Which Three Men Dispute about a Woman.</a>

There is "GoToPageNumber" event , how to change pager item position on click of this.

2 Answers 2

1

I have found solution. Set movement method to textview.

textview.setMovementMethod(LinkMovementMethodExt.getInstance());

public static class LinkMovementMethodExt extends LinkMovementMethod 
{
    private static LinkMovementMethod sInstance;

    public static MovementMethod getInstance()
    {
        if (sInstance == null)
        {
            sInstance = new LinkMovementMethodExt();
        }
        return sInstance;
    }

    int off = 0;
     @Override
     public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) 
     {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) 
        {
            try {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();

                x += widget.getScrollX();
                y += widget.getScrollY();

                Layout layout = widget.getLayout();
                int currentLine = layout.getLineForVertical(y);
                int totalLine = layout.getLineCount();
                off = layout.getOffsetForHorizontal(currentLine, x);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                URLSpan[] urls = buffer.getSpans(off, off+1, URLSpan.class);
                for(URLSpan span : urls) 
                {
                    String urlStr = span.getURL();

                    Log.v("URL SPAN", urlStr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can't run JavaScript in a TextView. You will have to instead do everything in java with click listeners. If you want only handle a click on only a link within a TextView, you will need to learn how to use a Spannable. You can get help on that here.

1 Comment

I am already using "HtmlSpanner" library to parse html and css , so I cannot use another Spannable like you suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.