Example usage for android.text Layout getOffsetForHorizontal

List of usage examples for android.text Layout getOffsetForHorizontal

Introduction

In this page you can find the example usage for android.text Layout getOffsetForHorizontal.

Prototype

public int getOffsetForHorizontal(int line, float horiz) 

Source Link

Document

Get the character offset on the specified line whose position is closest to the specified horizontal position.

Usage

From source file:Main.java

public static void setTextWithLinks(TextView textView, String htmlText) {
    setHtmlText(textView, htmlText);/*  ww w.  j  av a2 s .  co  m*/
    textView.setOnTouchListener((v, event) -> {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

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

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

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = Spannable.Factory.getInstance().newSpannable(widget.getText()).getSpans(off,
                    off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                }
                return true;
            }
        }
        return false;
    });
}

From source file:io.github.hidroh.materialistic.AppUtils.java

public static void setTextWithLinks(TextView textView, CharSequence html) {
    textView.setText(html);//  www .ja va 2  s  .c  o m
    // TODO https://code.google.com/p/android/issues/detail?id=191430
    //noinspection Convert2Lambda
    textView.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

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

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

                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] link = Spannable.Factory.getInstance().newSpannable(widget.getText())
                        .getSpans(off, off, ClickableSpan.class);

                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        link[0].onClick(widget);
                    }
                    return true;
                }
            }
            return false;
        }
    });
}

From source file:com.waz.zclient.pages.main.conversation.views.row.message.views.TextMessageWithTimestamp.java

private boolean isClickInsideLink(MotionEvent event) {
    Object text = messageTextView.getText();
    if (text != null && text instanceof Spanned) {
        Spannable buffer = (Spannable) text;

        int x = (int) event.getX();
        int y = (int) event.getY();

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

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

        Layout layout = messageTextView.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        return link.length > 0;

    }/*from w  w  w.jav  a2  s  . co  m*/
    return false;
}

From source file:com.example.pdftranslator.ScreenSlidePageFragment.java

private void determineWordSelected(View v, MotionEvent event) {

    Layout layout = ((TextView) v).getLayout();
    int x = (int) event.getX();
    int y = (int) event.getY();

    Log.i("NAME", Thread.currentThread().getId() + " in touch event fragment" + " " + event.toString());

    if (layout != null) {

        //   Log.i( "NAME", ActivityTextDisplayer.letter_index.values().toString());
        int line = layout.getLineForVertical(y);
        int offset = layout.getOffsetForHorizontal(line, x);
        int copy_offset = offset;
        String word = ActivityTextDisplayer.letter_index.get(offset);

        //    Log.i("NAME", ""+offset + " ---" + word + "--- " ); 
        while (true) {
            copy_offset--;/*w  w  w.  j  a  va2s . co  m*/
            if (ActivityTextDisplayer.letter_index.get(copy_offset).equals(" ")
                    || ActivityTextDisplayer.letter_index.get(copy_offset).equals("\n")
                    || ActivityTextDisplayer.letter_index.get(copy_offset).equals("."))
                break;
            word = ActivityTextDisplayer.letter_index.get(copy_offset) + word;
        }

        copy_offset = offset;

        while (true) {
            copy_offset++;
            if (ActivityTextDisplayer.letter_index.get(copy_offset).equals(" ")
                    || ActivityTextDisplayer.letter_index.get(copy_offset).equals("\n")
                    || ActivityTextDisplayer.letter_index.get(copy_offset).equals("."))
                break;
            word = word + ActivityTextDisplayer.letter_index.get(copy_offset);
        }

        String params[] = { word, Language.ENGLISH.toString(), Language.ROMANIAN.toString() };

        new InternetConnection(getActivity()).execute(params);
        Log.i("NAME", "" + offset + " " + line + " ------" + ActivityTextDisplayer.letter_index.get(offset)
                + "-----   -----" + word + "------");
    }

}

From source file:org.openintents.notepad.NoteEditor.java

@Override
protected void onResume() {
    super.onResume();
    if (DEBUG) {/*from   w ww  . j a  v  a 2  s .  c o  m*/
        Log.d(TAG, "onResume");
    }

    if (DEBUG) {
        Log.d(TAG, "mDecrypted: " + mDecryptedText);
    }

    // Set auto-link on or off, based on the current setting.
    int autoLink = PreferenceActivity.getAutoLinkFromPreference(this);

    mText.setAutoLinkMask(autoLink);

    mEncrypted = 0;

    if (mState == STATE_EDIT || (mState == STATE_INSERT) || mState == STATE_EDIT_EXTERNAL_NOTE) {
        getNoteFromContentProvider();
    } else if (mState == STATE_EDIT_NOTE_FROM_SDCARD) {
        getNoteFromFile();
    }

    if (mEncrypted == 0 || mDecryptedText != null) {
        applyInsertText();
    }

    // Make sure that we don't use the link movement method.
    // Instead, we need a blend between the arrow key movement (for regular
    // navigation) and
    // the link movement (so the user can click on links).
    mText.setMovementMethod(new ArrowKeyMovementMethod() {
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            // This block is copied and pasted from LinkMovementMethod's
            // onTouchEvent (without the part that actually changes the
            // selection).
            int action = event.getAction();

            if (action == MotionEvent.ACTION_UP) {
                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 line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

                if (link.length != 0) {
                    link[0].onClick(widget);
                    return true;
                }
            }

            return super.onTouchEvent(widget, buffer, event);
        }
    });

    setTheme(loadTheme());

    startupSearch();
}

From source file:org.telegram.ui.ChatActivity.java

private boolean spanClicked(ListView list, View view, int textViewId) {
    final TextView widget = (TextView) view.findViewById(textViewId);
    if (widget == null) {
        return false;
    }//from   w w  w  . j  ava2 s . c o  m
    try {
        list.offsetRectIntoDescendantCoords(widget, mLastTouch);
        int x = mLastTouch.right;
        int y = mLastTouch.bottom;

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();

        final Layout layout = widget.getLayout();
        if (layout == null) {
            return false;
        }
        final int line = layout.getLineForVertical(y);
        final int off = layout.getOffsetForHorizontal(line, x);

        final float left = layout.getLineLeft(line);
        if (left > x || left + layout.getLineWidth(line) < x) {
            return false;
        }

        final Editable buffer = new SpannableStringBuilder(widget.getText());
        if (buffer == null) {
            return false;
        }
        final ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length == 0) {
            return false;
        }

        link[0].onClick(widget);
        return true;
    } catch (Exception e) {
        FileLog.e("tmessages", e);
        return false;
    }
}