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

//Got But Without Pattern Match

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



    ClickableSpan clickableSpanstar1stWarsHashText = new ClickableSpan() {
        @Override
        public void onClick(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();
        }
    };

    ClickableSpan clickableSpanmustWatchHashText = new ClickableSpan() {
        @Override
        public void onClick(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();
        }
    };

    ClickableSpan clickableSpanstar2ndWarsHashText = new ClickableSpan() {
        @Override
        public void onClick(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");

    TextView  textView = (TextView) findViewById(R.id.textView); //textView id i.e. android:id="@+id/textView"
    textView.setText(hashText);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(Color.TRANSPARENT);

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


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());

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.

private void setTags(TextView pTextView, String pTagString) {
    SpannableString string = new SpannableString(pTagString);

    int start = -1;

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

        } else if (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(new ClickableSpan() {

                    @Override
                    public void onClick(View widget) {

                        Toast.makeText(mContext, "" + tag, 3000)
                                .show();
                    }
                    @Override
                    public void updateDrawState(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.