Kuldeep Sakhiya
Kuldeep Sakhiya

Reputation: 3252

Android : Use javascript in textview

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.

Upvotes: 0

Views: 1673

Answers (2)

Kuldeep Sakhiya
Kuldeep Sakhiya

Reputation: 3252

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;
    }
}

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317467

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.

Upvotes: 0

Related Questions