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:-
But I want to like this:-
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With