Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Setting onClickListener to a Part of text in a TextView - Issue

I am trying to recognise hashtags in my TextView and make them clickable such that I can take the user to another View when they click on the Hashtag.

I managed to identify Hashtags in the TextView using Pattern Matching and they appear colored in Runtime. However, I need to make the Hashtag clickable.

Here's my Code:

 SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");
 Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);

 while (matcher.find())
 {
          hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#000763")), matcher.start(), matcher.end(), 0);
          String tag = matcher.group(0);
 }

 holder.caption.setText(hashText);

 //I need to set an OnClick listener to all the Hashtags recognised

Using the same solution above, how can I add onclick listeners to every hashtag?

like image 418
Dinuka Jay Avatar asked Dec 31 '15 05:12

Dinuka Jay


People also ask

Can TextView have onClick Android?

as you're telling in your xml to capture the click for the text view ( android:onClick="onClick" ) in onClick module, you don't need to add an onClick listener in your java code. Show activity on this post. Show activity on this post.

Which method is used to set the text in a TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method.


Video Answer


2 Answers

there is a way... after seeing your question i was just googling .. and i found this, i hope it will work...

1. you can use android.text.style.ClickableSpan link

SpannableString ss = new SpannableString("Hello World");
    ClickableSpan span1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do some thing
        }
    };

    ClickableSpan span2 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            // do another thing
        }
    };

    ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

another way.. link

 TextView myTextView = new TextView(this);
    String myString = "Some text [clickable]";
    int i1 = myString.indexOf("[");
    int i2 = myString.indexOf("]");
    myTextView.setMovementMethod(LinkMovementMethod.getInstance());
    myTextView.setText(myString, BufferType.SPANNABLE);
    Spannable mySpannable = (Spannable)myTextView.getText();
    ClickableSpan myClickableSpan = new ClickableSpan()
    {
     @Override
     public void onClick(View widget) { /* do something */ }
    };
    mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

answer just copied from those link...

like image 92
Tanim reja Avatar answered Oct 06 '22 01:10

Tanim reja


Yes you can do it, you need to use ClickableSpan with SpannableString

paste this code inside your while loop

final String tag = matcher.group(0);
ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View textView) {
                    Log.e("click","click " + tag);
                }
                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);

                }
            };
            hashText.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Dont forget to set setMovementMethod() on your TextView

holder.caption.setMovementMethod(LinkMovementMethod.getInstance());
like image 35
Ravi Avatar answered Oct 06 '22 02:10

Ravi