Skip to content Skip to sidebar Skip to footer

Clickablespan Not Clickable In Custom View (not Textview)

In our app we want to improve scroll speed via drawing custom views with text. Problem is ClickableSpan not clicked. For TextView we can use textView.setMovementMethod(LinkMovemen

Solution 1:

Try this:

Here is my sample code

 textView.setText(addClickablePart(), TextView.BufferType.SPANNABLE);

         private SpannableStringBuilder addClickablePart() {
    Stringstr="I know just how to whisper, And I know just how to cry,I know just where to find the answers. Click Me";
                    SpannableStringBuilderssb=newSpannableStringBuilder(str);
                    finalForegroundColorSpanfcs=newForegroundColorSpan(ContextCompat.getColor(AppController.getContext(), R.color.dark_green));

                    finalStringclickString="Click Me";
                    intidx1= str.indexOf(clickString);
                    intidx2= str.length() - 1;

                    ssb.setSpan(newClickableSpan() {

                        @OverridepublicvoidonClick(View widget) {
                            Toast.makeText(MainActivity.this, clickString,
                                    Toast.LENGTH_SHORT).show();                        
                        }
                    }, idx1, idx2, 0);

                    ssb.setSpan(fcs, idx1, idx2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

                    return ssb;
                }

Post a Comment for "Clickablespan Not Clickable In Custom View (not Textview)"