Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple click on textview android

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.

like image 873
Anand Jain Avatar asked Sep 22 '17 13:09

Anand Jain


2 Answers

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

like image 187
Rapunzel Van Winkle Avatar answered Sep 19 '22 11:09

Rapunzel Van Winkle


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.

like image 31
avez raj Avatar answered Sep 21 '22 11:09

avez raj