Posted on Leave a comment

Android: Multiple Click Actions on Single Text View

https://medium.com/viithiisys/android-multiple-click-actions-on-single-textview-c5fec71b6e25

private static void singleTextView(TextView textView, final String userName, String status, final String songName) {

        SpannableStringBuilder spanText = new SpannableStringBuilder();
        spanText.append(userName);
        spanText.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {

                // On Click Action

            }

            @Override
            public void updateDrawState(TextPaint textPaint) {
                textPaint.setColor(textPaint.linkColor);    // you can use custom color
                textPaint.setUnderlineText(false);    // this remove the underline
            }
        }, spanText.length() - userName.length(), spanText.length(), 0);

        spanText.append(status);
        spanText.append(songName);
        spanText.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {

                // On Click Action

            }

            @Override
            public void updateDrawState(TextPaint textPaint) {
                textPaint.setColor(textPaint.linkColor);    // you can use custom color
                textPaint.setUnderlineText(false);    // this remove the underline
            }
        },spanText.length() - songName.length(), spanText.length(), 0);

        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spanText, TextView.BufferType.SPANNABLE);

    }
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.