Example usage for android.widget TextView getLocationOnScreen

List of usage examples for android.widget TextView getLocationOnScreen

Introduction

In this page you can find the example usage for android.widget TextView getLocationOnScreen.

Prototype

public void getLocationOnScreen(@Size(2) int[] outLocation) 

Source Link

Document

Computes the coordinates of this view on the screen.

Usage

From source file:com.ravi.apps.android.newsbytes.MainActivity.java

/**
 * Adds the headline details into the intent extras.
 *//*  ww w  .  j  a v a  2 s  .  com*/
private void addHeadlineDetailsToIntent(TextView textView, Intent intent) {
    // Get the headline position.
    int[] location = new int[2];
    textView.getLocationOnScreen(location);
    float xPos = location[0];
    float yPos = location[1];

    // Get the text.
    String text = (String) textView.getText();

    // Add details into intent extras.
    intent.putExtra(TEXT_XPOS, xPos);
    intent.putExtra(TEXT_YPOS, yPos);
    intent.putExtra(TEXT, text);
}

From source file:com.juick.android.MessageMenu.java

public boolean onItemLongClick(final AdapterView parent, View view, final int position, long id) {
    menuActions.clear();/* w w w .  ja  v  a 2s .  com*/
    listSelectedItem = (JuickMessage) parent.getAdapter().getItem(position);
    JuickMessage secondaryItem = null;
    float x = JuickAdvancedApplication.getLastTouchX();
    float y = JuickAdvancedApplication.getLastTouchY();
    if (listSelectedItem.contextPost != null) {
        secondaryItem = listSelectedItem.contextPost;
    }

    urls = new ArrayList<String>();
    if (listSelectedItem.Photo != null) {
        urls.add(listSelectedItem.Photo);
    }
    if (listSelectedItem.Video != null) {
        urls.add(listSelectedItem.Video);
    }

    collectURLs(listSelectedItem.Text, listSelectedItem.getMID());
    if (secondaryItem != null) {
        collectURLs(secondaryItem.Text, listSelectedItem.getMID());
    }

    if (urls.size() > 0) {
        HashSet<String> added = new HashSet<String>();
        for (final String url : urls) {
            // filter out crap
            if (url.endsWith("hqdefault.jpg") && url.contains("youtube"))
                continue;
            // deduplicate
            if (added.add(url)) {
                menuActions.add(new RunnableItem(url) {
                    @Override
                    public void run() {
                        launchURL(listSelectedItem.getMID(), url);
                    }
                });
            }
        }
    }

    collectMenuActions();

    // Censor - Submit a token for review
    TextView listSelectedTextView = (TextView) view.findViewById(R.id.text);
    int[] loc2 = new int[2];
    listSelectedTextView.getLocationOnScreen(loc2);
    int[] loc1 = new int[2];
    listView.getLocationOnScreen(loc1);
    y -= loc2[1] - loc1[1];
    x -= loc2[0] - loc1[0];
    int offset = getOffsetForPosition(listSelectedTextView, x, y);
    if (offset != -1 && false) {
        final String text = listSelectedTextView.getText().toString();
        final String word = Utils.getWordAtOffset(text, offset);
        if (word != null) {
            menuActions.add(new RunnableItem(
                    activity.getResources().getString(R.string.CensorSubmitTokenForReview1) + " \"" + word
                            + "\" " + activity.getResources().getString(R.string.CensorSubmitTokenForReview2)) {
                @Override
                public void run() {
                    actionSubmitTokenForReview(word);
                }
            });
        } else {
            Toast.makeText(activity, activity.getResources().getString(R.string.CensorSubmitWordSelectionError),
                    Toast.LENGTH_LONG).show();
        }
    }
    runActions();
    return true;
}

From source file:com.nttec.everychan.ui.presentation.BoardFragment.java

private Point getSpanCoordinates(View widget, ClickableURLSpan span) {
    TextView parentTextView = (TextView) widget;

    Rect parentTextViewRect = new Rect();

    // Initialize values for the computing of clickedText position
    SpannableString completeText = (SpannableString) (parentTextView).getText();
    Layout textViewLayout = parentTextView.getLayout();

    int startOffsetOfClickedText = completeText.getSpanStart(span);
    int endOffsetOfClickedText = completeText.getSpanEnd(span);
    double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal(startOffsetOfClickedText);
    double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal(endOffsetOfClickedText);

    // Get the rectangle of the clicked text
    int currentLineStartOffset = textViewLayout.getLineForOffset(startOffsetOfClickedText);
    int currentLineEndOffset = textViewLayout.getLineForOffset(endOffsetOfClickedText);
    boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;
    textViewLayout.getLineBounds(currentLineStartOffset, parentTextViewRect);

    // Update the rectangle position to his real position on screen
    int[] parentTextViewLocation = { 0, 0 };
    parentTextView.getLocationOnScreen(parentTextViewLocation);

    double parentTextViewTopAndBottomOffset = (parentTextViewLocation[1] - parentTextView.getScrollY()
            + parentTextView.getCompoundPaddingTop());

    Rect windowRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowRect);
    parentTextViewTopAndBottomOffset -= windowRect.top;

    parentTextViewRect.top += parentTextViewTopAndBottomOffset;
    parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;

    parentTextViewRect.left += (parentTextViewLocation[0] + startXCoordinatesOfClickedText
            + parentTextView.getCompoundPaddingLeft() - parentTextView.getScrollX());
    parentTextViewRect.right = (int) (parentTextViewRect.left + endXCoordinatesOfClickedText
            - startXCoordinatesOfClickedText);

    int x = (parentTextViewRect.left + parentTextViewRect.right) / 2;
    int y = (parentTextViewRect.top + parentTextViewRect.bottom) / 2;
    if (keywordIsInMultiLine) {
        x = parentTextViewRect.left;/*from  www. j  a  v a  2  s .c  om*/
    }

    return new Point(x, y);
}