How to Know What Part of a TextView was Selected

288 views Asked by At

I am populating textviews on the activity dynamically, based on text that is being parsed in from xml articles. The text is formatted using html tags, so I set the text like:

textViewBody.setText(Html.fromHtml(content));

The articles are set up to either some text in a body to a website, to another local article, or to simply display text. Before, we only had 1 link at the most, so I used this code:

textViewBody.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (textViewBody.getUrls().length != 0) {
                        String link = textViewBody.getUrls()[0].getURL();

                        Intent i;

                        if (link.startsWith("article")) {
                            i = new Intent(getActivity().getApplicationContext(), ArticleActivity.class);
                            i.putExtra("file", link.substring(10));
                            i.putExtra("title", parentView.article.title);
                            startActivity(i);
                        } else if (link.startsWith("http")) {
                            i = new Intent(Intent.ACTION_VIEW);
                            i.setData(Uri.parse(link));
                            startActivity(i);
                        }
                    } else {
                        // Regular body text was selected.
                    }
                }
            });

This worked perfectly, but now that some of the articles have more than one link it only opens the first one (as that's what I set it to do). My question is, how can I set the textview to know what link I have clicked on? Again, this text is dynamic (and there are around 200+ xml articles), so there's no way I can just check the contents of the string against a known list of links.

It may be important to mention that this is being done on a fragment (ArticleActivity is a tabHost, with one tab displaying the text of the article).

2

There are 2 answers

1
Gabe Sechan On BEST ANSWER

Don't even try to do this via onTouch. Use Spannables, and put each link inside a clickable span- http://developer.android.com/reference/android/text/style/ClickableSpan.html. Then when the text is clicked, its onClick method will be called and it can perform whatever action it needs to.

0
DolDurma On

I think this is simple way to get selected text and found start and end selected text on textview

TextView mTextView = findViewById(R.id.textView);
if (mTextView.isFocused()) {
    final int selStart = mTextView.getSelectionStart();
    final int selEnd   = mTextView.getSelectionEnd();
    int min = Math.max(0, Math.min(selStart, selEnd));
    int max = Math.max(0, Math.max(selStart, selEnd));
    final CharSequence selectedText = mTextView.getText().subSequence(min, max);
    Toast.makeText(getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
    selected = selectedCafeWeb;
    Toast.makeText(getBaseContext(), selected + " " + selectedText.toString(), Toast.LENGTH_SHORT);
}