Skip to content Skip to sidebar Skip to footer

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 Hashtag

Solution 1:

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.ClickableSpanlink

SpannableStringss=newSpannableString("Hello World");
    ClickableSpanspan1=newClickableSpan() {
        @OverridepublicvoidonClick(View textView) {
            // do some thing
        }
    };

    ClickableSpanspan2=newClickableSpan() {
        @OverridepublicvoidonClick(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

TextViewmyTextView=newTextView(this);
    StringmyString="Some text [clickable]";
    inti1= myString.indexOf("[");
    inti2= myString.indexOf("]");
    myTextView.setMovementMethod(LinkMovementMethod.getInstance());
    myTextView.setText(myString, BufferType.SPANNABLE);
    SpannablemySpannable= (Spannable)myTextView.getText();
    ClickableSpanmyClickableSpan=newClickableSpan()
    {
     @OverridepublicvoidonClick(View widget) { /* do something */ }
    };
    mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

answer just copied from those link...

Solution 2:

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

paste this code inside your while loop

finalStringtag= matcher.group(0);
ClickableSpanclickableSpan=newClickableSpan() {
                @OverridepublicvoidonClick(View textView) {
                    Log.e("click","click " + tag);
                }
                @OverridepublicvoidupdateDrawState(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());

Solution 3:

You can change the color with clickable any key(@,#) text from textview by using this code.I use this formate.It work well in my side.

privatevoidsetTags(TextView pTextView, String pTagString) {
    SpannableStringstring = newSpannableString(pTagString);

    int start = -1;

    for (int i = 0; i < pTagString.length(); i++) {
        if (pTagString.charAt(i) == '@' || pTagString.charAt(i) == '#') {               
            start = i;

        } elseif (pTagString.charAt(i) == ' '
                || (i == pTagString.length() - 1 && start != -1)) {
            if (start != -1) {
                if (i == pTagString.length() - 1) {
                    i++; // case for if hash is last word and there is no// space after word
                }

                final String tag = pTagString.substring(start, i);              
                string.setSpan(newClickableSpan() {

                    @OverridepublicvoidonClick(View widget) {

                        Toast.makeText(mContext, "" + tag, 3000)
                                .show();
                    }
                    @OverridepublicvoidupdateDrawState(TextPaint ds) {

                        if (tag.contains("@"))
                            ds.setColor(Color.parseColor("#0474be"));
                        else
                            ds.setColor(Color.parseColor("#ed6057"));

                        ds.setUnderlineText(false);
                    }
                }, start, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                start = -1;
            }
        }
    }

    pTextView.setMovementMethod(LinkMovementMethod.getInstance());
    pTextView.setText(string);
}

Please try this way hope you can solve your problem.

Solution 4:

//Got But Without Pattern Match

SpannableStringhashText=newSpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");



    ClickableSpanclickableSpanstar1stWarsHashText=newClickableSpan() {
        @OverridepublicvoidonClick(View widget) {
            //Intent starWars = new Intent(MainActivity.this,starWars.class); //starWars is a class file which extends Activity//startActivity(starWars);
            Toast.makeText(MainActivity.this,"Clicked On 1st #StarWars Remove comments of above line",Toast.LENGTH_LONG).show();
        }
    };

    ClickableSpanclickableSpanmustWatchHashText=newClickableSpan() {
        @OverridepublicvoidonClick(View widget) {
            //Intent mustWatch = new Intent(MainActivity.this,mustWatch.class); //starWars is a class file which extends Activity//startActivity(mustWatch);
            Toast.makeText(MainActivity.this,"Clicked On #MustWatch Remove comments of above line",Toast.LENGTH_LONG).show();
        }
    };

    ClickableSpanclickableSpanstar2ndWarsHashText=newClickableSpan() {
        @OverridepublicvoidonClick(View widget) {
            //Intent starWars = new Intent(MainActivity.this,starWars.class); //starWars is a class file which extends Activity//startActivity(starWars);
            Toast.makeText(MainActivity.this,"Clicked On 2nd #StarWars Remove comments of above line",Toast.LENGTH_LONG).show();
        }
    };

    hashText.setSpan(clickableSpanstar1stWarsHashText,15,24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    hashText.setSpan(clickableSpanmustWatchHashText, 55, 65, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    hashText.setSpan(clickableSpanstar2ndWarsHashText,66,75, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);




    //SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");TextViewtextView= (TextView) findViewById(R.id.textView); //textView id i.e. android:id="@+id/textView"
    textView.setText(hashText);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(Color.TRANSPARENT);

Solution 5:

You can use android.text.style.ClickableSpan for this: Refer answer

Post a Comment for "Android: Setting Onclicklistener To A Part Of Text In A Textview - Issue"