15

I want to set multiple click on textview. I see many answers but any answer can't help me. I make spanned string using this code:-

  private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(Html.fromHtml(deafultSpna + feelingSpan+tagfriendspan));
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click",
                    Toast.LENGTH_SHORT).show();
        }
    }, 0, Html.fromHtml(deafultSpna).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click ffff",
                    Toast.LENGTH_SHORT).show();
        }
    }, Html.fromHtml(deafultSpna).length(), Html.fromHtml(deafultSpna+feelingSpan).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(AddPostActivity.this, "hello click ffff fdsfds",
                    Toast.LENGTH_SHORT).show();
        }
    }, Html.fromHtml(deafultSpna+feelingSpan).length(), Html.fromHtml(deafultSpna+feelingSpan+tagfriendspan).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ssb;
}

but using this code I found like this view:- My layout look this

But I want to like this:-

enter image description here

I make spanned using this codefeelingSpan = "<font color=#414141> - Feeling </font><font color=#bd2436>" + feeling_name + "</font>";

Any one can help me so solve this problem. Thanks in advance.

Rob
  • 2,133
  • 3
  • 29
  • 39
Anand Jain
  • 2,285
  • 6
  • 37
  • 66
  • a little confused by your question. you have a textview..and you want to be able to click it a few times and each click does something? – letsCode Sep 22 '17 at 13:20
  • 4
    Possible duplicate of [How to set the part of the text view is clickable](https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable) and [How to click or tap on a TextView text on different words?](https://stackoverflow.com/questions/9584136/how-to-click-or-tap-on-a-textview-text-on-different-words) – Adinia Sep 22 '17 at 13:20
  • @DroiDev I want click on each span and this work fine but my each span color are same. I want to look like above image. – Anand Jain Sep 22 '17 at 13:43
  • gotcha. wish i could help but i never did anything like that before. good luck. – letsCode Sep 22 '17 at 13:53
  • Take a look at this answer [enter link description here](https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable) – the Luckiest Nov 07 '17 at 22:08
  • Try to Wrap your whole html in a

    – skryshtafovych Nov 08 '17 at 00:02
  • https://stackoverflow.com/questions/16510940/android-multiple-clickable-strings-in-textview – Ajay Venugopal Nov 13 '17 at 12:20

2 Answers2

8

To make things easier, we can start by defining a small class that will make the clickable spans red and show the appropriate message when clicked.

private class MyClickableSpan extends ClickableSpan {
    String text;

    MyClickableSpan(String text) {
        this.text = text;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false); // get rid of underlining
        ds.setColor(Color.RED);     // make links red
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), text, Toast.LENGTH_SHORT).show();
    }
}

Next, we can add a small convenience method to create the clickable spans.

private void addClickableText(SpannableStringBuilder ssb, int startPos, String clickableText, String toastText) {
    ssb.append(clickableText);
    ssb.setSpan(new MyClickableSpan(toastText), startPos, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Finally, we can fill the spannable string and display it.

    TextView text = findViewById(R.id.text);

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    addClickableText(ssb, ssb.length(), "Abhishek Nagar", "hello click");
    ssb.append(" - Feeling ");
    addClickableText(ssb, ssb.length(), "Sad", "hello click ffff");
    ssb.append(" with ");
    addClickableText(ssb, ssb.length(), "Anand Jainb", "hello click ffff fdsfds");

    text.setMovementMethod(LinkMovementMethod.getInstance());   // make our spans selectable
    text.setText(ssb);

And that's all there is to it!

screenshot

Rapunzel Van Winkle
  • 5,150
  • 6
  • 30
  • 47
2
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.txt1);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(addClickablePart(getString(R.string.amanda_bio2)));
    }


    private SpannableStringBuilder addClickablePart(String str) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(str);

        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "span clicked 1", Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.RED);
            }
        }, 200, 214, 0);

        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "span clicked 2", Toast.LENGTH_LONG).show();

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);
            }
        }, 241, 255, 0);
        return ssb;
    }
}

You can even add multiple spans and also customize its colors.

avez raj
  • 1,815
  • 2
  • 18
  • 33