Ad
How To Find Particular Word From Full Text And Make It Hyperlink Clickable With Textview?
I am using textview and setting text which I get from json,
"This is our contact us page please call us"
the above text I am getting from server and it displays on 3rd recyclerview item, now what I am trying is in any listitem, if 'contact' word is there, I am trying to make it as hyperlink and want to make it clickable. but it is not working.
String fulltext=brandList.get(position).getFAQAnswerText();
String match="contact";
if (fulltext.contains(match)) {
System.out.println("Keyword matched the string" );
ss = new SpannableString(fulltext);
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
holder.tvans.setText(ss);
holder.tvans.setMovementMethod(LinkMovementMethod.getInstance());
Ad
Answer
textView.setText("this is clickable text");
if (fulltext.contains("clickable")) {
setClickableHighLightedText(textView, "clickable", new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: do your stuff here
}
});
}
Put this method in your Util class.
/**
* use this method to set clickable highlighted a text in TextView
*
* @param tv TextView or Edittext or Button or child of TextView class
* @param textToHighlight Text to highlight
*/
public void setClickableHighLightedText(TextView tv, String textToHighlight, View.OnClickListener onClickListener) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
if (onClickListener != null) onClickListener.onClick(textView);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(tv.getContext().getResources().getColor(R.color.colorPrimary));
ds.setUnderlineText(true);
}
};
SpannableString wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
Output
Update
Change span color
@Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(tv.getContext().getResources().getColor(R.color.spanColor)); }
Change highlight / onTouch color
textView.setHighlightColor(getResources().getColor(R.color.onTouchColor));
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad