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.
I Have Created a generalized function for this purpose hope this helps
fun TextView.makeLinks(vararg links: Triple<String, View.OnClickListener,LinkProperties>) {
try {
val spannableString = SpannableString(this.text)
for (link in links) {
val clickableSpan = object : ClickableSpan() {
override fun updateDrawState(textPaint: TextPaint) {
super.updateDrawState(textPaint)
textPaint.isUnderlineText = link.third.isUnderlineText
textPaint.isFakeBoldText = link.third.isBoldText
textPaint.color = link.third.color ?: Color.parseColor(DCColorPicker.BLACK)
}
override fun onClick(view: View) {
Selection.setSelection((view as TextView).text as Spannable, 0)
view.invalidate()
link.second.onClick(view)
}
}
val startIndexOfLink = this.text.toString().indexOf(link.first)
spannableString.setSpan(
clickableSpan, startIndexOfLink, startIndexOfLink + link.first.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
this.movementMethod = LinkMovementMethod.getInstance() // without LinkMovementMethod, link can not click
this.setText(spannableString, TextView.BufferType.SPANNABLE)
} catch (e: Exception) {
Log.e(TAG, "makeLinks: " + e.message)
}
}
and you can add N no. of properties here
data class LinkProperties(val isUnderlineText: Boolean = false, val isBoldText: Boolean = false, val color: Int?=null)
and for calling you can use this function like this
val linkBoldOnly = LinkProperties(isBoldText = true)
val linkBoldUnderline = LinkProperties(isUnderlineText = true, isBoldText = true)
binding.name.makeLinks(
Triple("text", View.OnClickListener { openMore() }, linkBoldUnderline),
Triple(name.toString(), View.OnClickListener { itemClick() }, linkBoldOnly)
)
Related
I am trying to expand and collapse the Text Views in RecyclerView and I can perform this task easily but the requirement is to expand one Text View at a time i.e. if Text View of 1st position in RecyclerView is expanded and Text View of second position is clicked then 1st one should automatically collapse.
This are the methods to expand and collapse:
private void addReadMore(final String text, final TextView textView) {
SpannableString ss = new SpannableString(text.substring(0, 280) + "... Read More");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
addReadLess(text, textView);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
} else {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
}
}
};
ss.setSpan(clickableSpan, ss.length() - 10, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
private void addReadLess(final String text, final TextView textView) {
SpannableString ss = new SpannableString(text + " Read Less");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
addReadMore(text, textView);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
} else {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
}
}
};
ss.setSpan(clickableSpan, ss.length() - 10, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
Please suggest me how can I expand only 1 Text View at a time.
Thanks in advance.
You can keep the value of the expanded index in a variable.
And in your onClickListener where you expand the view, you call notifyDataSetChanged.
Create a global variable ( let it be int expandedIndex = -1)
And everytime you call read more or read less function, update this variable and call notifyDataSetChanged.
Edited
Note: index is the position of the position of item in the adapter.
int expandedIndex=-1;
void onBindViewHolder(){
if(length>280 && position==-1){ //for first time, call read more }
else if(length<280&&position==-1){ //Call read less) }
else { //Call read more when manual clicks are done on position }
}
private void addReadMore(final String text, final TextView textView) {
SpannableString ss = new SpannableString(text.substring(0, 280) + "... Read More");
}
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
position=-1;
notifiyDatasetChanged()
addReadLess(text, textView);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
} else {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
}
}
};
ss.setSpan(clickableSpan, ss.length() - 10, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
private void addReadLess(final String text, final TextView textView,int index) {
SpannableString ss = new SpannableString(text + " Read Less");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
position=index;
notifiyDatasetChanged
addReadMore(text, textView);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
} else {
ds.setColor(ContextCompat.getColor(mContext, R.color.txt_color));
}
}
};
ss.setSpan(clickableSpan, ss.length() - 10, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
I have a link in android textview. I am not able to capture the link click event.
String text = "http:://www.google.com is a google link";
textview.setText(text);
"http:://www.google.com" this span of string is clickable in textview. I want to capture that particular click event.
I tried the following.
public static void setTextView(TextView text, CharSequence sequence) {
UoloLogger.i(TAG, "Setting string :: "+sequence);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
UoloLogger.i(TAG, span.getURL());
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
I started setting text into my textview using setTextView() method. I am getting URLSpan array is empty even if i am having the links.
String text = "http:://www.google.com is a google link";
setTextView(textView, text);
Sorry for the bad english. I think, i have explained my problem. Can someone help me.
public static void setLinkclickEvent(TextView tv, HandleLinkClickInsideTextView clickInterface) {
String text = tv.getText().toString();
String str = "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])";
Pattern pattern = Pattern.compile(str);
Matcher matcher = pattern.matcher(tv.getText());
while (matcher.find()) {
int x = matcher.start();
int y = matcher.end();
final android.text.SpannableString f = new android.text.SpannableString(
tv.getText());
InternalURLSpan span = new InternalURLSpan();
span.setText(text.substring(x, y));
span.setClickInterface(clickInterface);
f.setSpan(span, x, y,
android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(f);
}
tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setFocusable(false);
}
public static class InternalURLSpan extends android.text.style.ClickableSpan {
private String text;
private HandleLinkClickInsideTextView clickInterface;
#Override
public void onClick(View widget) {
getClickInterface().onLinkClicked(getText());
}
public void setText(String textString) {
this.text = textString;
}
public String getText() {
return this.text;
}
public void setClickInterface(HandleLinkClickInsideTextView clickInterface) {
this.clickInterface = clickInterface;
}
public HandleLinkClickInsideTextView getClickInterface() {
return this.clickInterface;
}
}
public interface HandleLinkClickInsideTextView {
public void onLinkClicked(String url);
}
After this i just used the method send click event.
textview.setText("http://google.com is google website and http://youtube.com is youtube site");
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
public void onLinkClicked(String url) {
// Here I added my code
}
});
You can achieved the same using SpannableStringBuilder.
Simply initialize the TextView that you want to add 2 or more listeners and then pass that to the following method that I have created:
SAMPLE CODE:
private void customTextView(TextView view) {
SpannableStringBuilder spanTxt = new SpannableStringBuilder(
"I agree to the ");
spanTxt.append("Term of services");
spanTxt.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(), "Terms of services Clicked",
Toast.LENGTH_SHORT).show();
}
}, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
spanTxt.append(" and");
spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
spanTxt.append(" Privacy Policy");
spanTxt.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
Toast.LENGTH_SHORT).show();
}
}, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(spanTxt, BufferType.SPANNABLE);
}
And in your XML, use android:textColorLink to add custom link color of your choice. Like this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColorLink="#C36241" />
If you want to open a link after textview click, there are two options:
Using java code:
Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>");
textView.setText(text);
Uri uri = Uri.parse("http://shopwhere.com.au/");
Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
// Create and start the chooser
Intent chooser = Intent.createChooser(webIntent, "Open with");
startActivityForResult(chooser,0);
Using XML:
Use android:autoLink="web" inside textview tag. You can also change link color android:textColorHighlight="#android:color/transparent" and android:textColorLink="#color/white".
I am having a difficulty in setting a ClickListener in specific part of Text in SpannableTextView in android.
I have this text:
By registering you agree to the Terms And Services
I want to set clicklistener to the bold part only? Is it possible?
Yes, it's possible.
private void prepareTerms(){
String termsAndConditionsText = ...;
String discoverTermsText = ...;
SpannableString ss = new SpannableString(discoverTermsText);
int index = discoverTermsText.indexOf(termsAndConditionsText);
ss.setSpan(
new ClickableSpan() {
#Override
public void onClick(View widget) {
// open a webview here if you want
}
},
index,
index + termsAndConditionsText.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
);
mTermAndCondTextView.setText(ss);
mTermAndCondTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
// click function
}
}, start_index, end_index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(builder);
Try this code :
String text1 = "By registering you agree to the" + " ";
String text2 = "Terms And Services";
SpannableString spannableString = new SpannableString(text1 + text2);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
// listen click here
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
ds.setColor(Color.BLACK);
}
};
spannableString.setSpan(clickableSpan, text1.length(), text1.length() + text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spannableString);
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setHighlightColor(Color.BLACK);
You can use the method setSpan from the class android.text.Spannable
Complete example and necessary information provided in the official documentation
In your string "By registering you agree to the Terms And Services" start value would be 35 and end would be stringObj.length
I'm working on ClickableSpan in a TextView, and I'm trying to get the clicked span's text. This is my code.
// this is the text we'll be operating on
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
// make "dolor" (characters 12 to 17) display a toast message when touched
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
// This will get "Lorem ipsum dolor sit amet", but I just want "dolor"
String text = ((TextView) view).getText().toString();
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
};
text.setSpan(clickableSpan, 12, 17, 0);
As you can see, I set the clickablespan to the TextView from characters 12 to 17, and I want to get these characters in the onClick event.
Is there anyway I can do that? Or at least can I pass the 12, 17 parameter to onClick event?
Thank you!
try this:
public class LoremIpsumSpan extends ClickableSpan {
#Override
public void onClick(View widget) {
// TODO add check if widget instanceof TextView
TextView tv = (TextView) widget;
// TODO add check if tv.getText() instanceof Spanned
Spanned s = (Spanned) tv.getText();
int start = s.getSpanStart(this);
int end = s.getSpanEnd(this);
Log.d(TAG, "onClick [" + s.subSequence(start, end) + "]");
}
}
A little simpler, could also pass a model reference if necessary.
public class SpecialClickableSpan extends ClickableSpan {
String text;
public SpecialClickableSpan(String text){
super();
this.text = text;
}
#Override
public void onClick(View widget) {
Log.d(TAG, "onClick [" + text + "]");
}
}
Then call new SpecialClickableSpan("My Text")
Edited: previous code was wrong, this works
// make "dolor" (characters 12 to 17) display a toast message when touched
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
TextView textView = (TextView) view;
CharSequence charSequence = textView.getText();
if (charSequence instanceof Spannable) {
Spannable spannableText = (Spannable)charSequence;
ClickableSpan[] spans = spannableText.getSpans(0, textView.length(), ClickableSpan.class);
for (ClickableSpan span : spans) {
int start = spannableText.getSpanStart(span);
int end = spannableText.getSpanEnd(span);
Toast.makeText(MainActivity.this, charSequence.subSequence(start, end), Toast.LENGTH_LONG).show();
}
}
}
};
You can also use to make string spannable like this
String htmlLinkText = "Lorem ipsum <a href='http://www.google.com'>dolor</a> sit amet";
testView.setText(Html.fromHtml(htmlLinkText));
testView.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = testView.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) testView.getText();
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();//should clear old spans
for (URLSpan url : urls) {
CustomerTextClick click = new CustomerTextClick(url.getURL());
style.setSpan(click, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
testView.setText(style);
}
and CustomerTextClick will be
private static class CustomerTextClick extends ClickableSpan {
private String mUrl;
CustomerTextClick(String url) {
mUrl = url;
}
#Override
public void onClick(View widget) {
// TODO Auto-generated method stub
//Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show();
// Do your action here
}
}
Tested and working code.
I have a Spannable Object with a Clickable Object set to it. When the Spannable String is displayed in the TextView it has blue text and a blue underline (indicating to the user that this Text is Clickable). My problem is how can I prevent appearing the blue underline in TextView?
Use the below code and try
String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
class MyClickableSpan extends ClickableSpan{// extend ClickableSpan
String clicked;
public MyClickableSpan(String string) {
super();
clicked = string;
}
#Override
public void onClick(View tv) {
Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
}
This works for me. No need to create custom ClickableSpan class. Just override updateDrawState(TextPaint ds).
SpannableString span = new SpannableString("Some text");
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor); // you can use custom color
ds.setUnderlineText(false); // this remove the underline
}
#Override
public void onClick(View textView) {
// handle click event
}
};
span.setSpan(clickSpan, 5, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span);
Raghunandan's answer works perfectly for me. Here is a pared-down version of it:
public abstract class NoUnderlineClickableSpan extends ClickableSpan {
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}
Override updateDrawState method of ClickableSpan class
String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
class MyClickableSpan extends ClickableSpan{// extend ClickableSpan
String clicked;
public MyClickableSpan(String string) {
// TODO Auto-generated constructor stub
super();
clicked =string;
}
public void onClick(View tv) {
Toast.makeText(MainActivity.this,clicked ,
Toast.LENGTH_SHORT).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
For changing color of spannable String
SpannableString ss = new SpannableString("android Stack Overflow");
ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannableStringObject.toString();
Edit
SpannableString ss = getYourSpannableString();
UnderlineSpan[] uspans = ss.getSpans(0, ss.length(), UnderlineSpan.class);
for (UnderlineSpan us : uspans) {
ss.removeSpan(us);
}
Will remove all the UnderlineSpans from the Spannable.
simplest way is
string1 = new SpannableString("By Tapping Register You Agree To The \nTerms And Conditions");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_SHORT).show();
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false); // this line removes underline
}
};
text_terms.setMovementMethod(LinkMovementMethod.getInstance());
string1.setSpan(clickableSpan,37,string1.length(),0);
text_terms.setText(string1);
Based on the answer of #Sai Gopi Me
in Kotlin:
val clickableSpan: ClickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
// some action here
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}
}
Try the below code to remove underlined and clicked event on multiple words in textview :
String str="Angelina Clapped For Lester Kang";
Spannable span = Spannable.Factory.getInstance().newSpannable(str);
// 0 to 8 start and index of Angelina
span.setSpan(new ClickableSpan(str), 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 21 to 32 start and index of Lester Kang
span.setSpan(new ClickableSpan(str), 21, 32, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(span);
class ClickableSpan extends ClickableSpan{
String clicked;
public ClickableSpan (String string) {
super();
}
public void onClick(View v) {
Toast.makeText(MyActivity.this,""+((TextView)v).getText().toString(),Toast.LENGTH_SHORT).show();
}
public void updateDrawState(TextPaint ds) {
// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
}
ANURAG RASTOGI's answer saved the day for me! I already have a formatted SpannableString on which I wanted to apply a ClickableSpan:
spannableString.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
// Do something...
}
// Prevent
// ds.setColor(ds.linkColor);
// ds.setUnderlineText(true);
// in: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/ClickableSpan.java
// from executing.
#Override
public void updateDrawState(#NonNull TextPaint ds) {
// super.updateDrawState(ds);
}
},0, spannableString.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
The updateDrawState overrides the updateDrawState in the ClickableSpan class in Android, and by not calling super.updateDrawState it will not get executed.
All text formatting already present in spannableString will be preserved.
If you do not want any pre-applied attributes do not call super.updateDrawState(). On overriding updateDrawState(object:Textpaint) with help of object you can apply or call different functions present in Text Paint.