I tried URLSpan in scannable string like this
private void setSmsText() {
SpannableString string = new SpannableString("Text with a url span");
string.setSpan(new URLSpan("http://www.developer.android.com"), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sms_text.setText(string);
sms_text.setMovementMethod(LinkMovementMethod.getInstance());
}
Here while commenting the setMovementMethod line the text appears to be clickable(url format)
but when uncommenting the text is not clickable
I am not able to navigate to the url by clicking on the text
Try this
SpannableString spannableString = new SpannableString(YOUR_TEXT);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
//your code like open webview or any other
}
};
spannableString.setSpan(clickableSpan, YOUR_TEXT_STARTING_INDEX,YOUR_TEXT_END_INDEX , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
let me know if this work or not
Related
I have a textview with autoLinks set to all. I want to skip numbers like "2018"
which are years, these numbers should not be highlighted. Is there a delimiter I can use in the text so that it skips those numbers while parsing?
Edit:
This issue happens only in Mi devices.
try this....
String s1="jan 2018,Saturday";
String replaceString=s1.replace("2018","");//replaces all occurrences of "2018" to ""
System.out.println(replaceString);
Output := jan ,Saturday.
Use Spanable String in this case to highlight Specific String.
Here is an example:
SpannableString spannableStr = new SpannableString(originalText);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
spannableStr.setSpan(foregroundColorSpan, 15, 30, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableTextView.setText(spannableStr);
Set color and starting string index and ending index.
For more detail check this link click this link
I searched for your answer try this
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
This requires a customized version of URLSpan which doesn't enable the TextPaint's "underline" property:
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
#Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
here is the link
Try This Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
highlightTv();
}
protected void highlightTv(){
// Specify the text/word to highlight inside TextView
String textToHighlight = "2018";
// Construct the formatted text
String replacedWith = "<font color='green'>" + textToHighlight + "</font>";
// Get the text from TextView
String originalString = textView.getText().toString();
// Replace the specified text/word with formatted text/word
String modifiedString = originalString.replaceAll(textToHighlight,replacedWith);
// Update the TextView text
mTextView.setText(Html.fromHtml(modifiedString));
}
I'm trying to achieve something like this image image twitter tweet. The user creates tweets with hash tags and links in it and Twitter app converts into click able links. Normal TextView can't achieve that. How do I create something like that? Please provide technical details.
Try this
You can use ClickableSpan to achieve this like this
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Toast.makeText(context,"clicked", Toast.LENGTH_SHORT).show();
}
};
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString str1 = new SpannableString("click me");
str1.setSpan(clickableSpan, 0, str1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
str1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, str1.length(), 0);
str1.setSpan(new UnderlineSpan(), 0, str1.length(), 0);
SpannableString str2 = new SpannableString("demo String 2 ");
builder.append(str2);
builder.append(str1);
Textview.setText(builder, TextView.BufferType.SPANNABLE);
Textview.setMovementMethod(LinkMovementMethod.getInstance());
i came into this wired situation, my code is as follow
LinearLayout ll = new LinearLayout(this);
TextView tv = new TextView(this);
ll.addView(tv);
tv.setText(Html.fromHtml("<a STYLE=\"text-decoration:none;\" href=\""
+ StringEscapeUtils.escapeJava(elem.getChildText("newsLink")) + "\">"
+ StringEscapeUtils.escapeJava(elem.getChildText("Title")) + "</a>"));
tv.setTextColor(Color.BLACK);
but the style="text-decoration:none" and tv.setTextColor(color.black)both not working, the link is still in blue with underscore, any hints on why they're not working? Thanks!
you can try this. such as
String content = "your <a href='http://some.url'>html</a> content";
Here is a concise way to remove underlines from hyperlinks:
Spannable s = (Spannable) Html.fromHtml(content);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
s.setSpan(new UnderlineSpan() {
public void updateDrawState(TextPaint tp) {
tp.setUnderlineText(false);
}
}, s.getSpanStart(u), s.getSpanEnd(u), 0);
}
tv.setText(s);
You can use Spannable and URLSpan here to remove hyperlink underline from your code
First of all make your anchor tag text into Spannable
Spannable spannedText = Spannable.Factory.getInstance().newSpannable(
Html.fromHtml(webLinkText));
Create new class URLSpanNoUnderline and extend it with URLSpan and override updateDrawState method. in that method you can set setUnderlineText to false
then use this method you can remove your link
public static Spannable removeUnderlines(Spannable p_Text) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
return p_Text;
}
For more information you can visit this link
I have a textview as like the following:
txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");
It is just a big text. So, I used marquee to scroll the text horizontally. that works fine. My Question is, How to invoke the click event while clicking the selected scrolling text .
Say for ex :
when user click the word "Registering" in the above textview, I have to invoke the new Intent.
When the user click on the word "Terms" , I have to
invoke another new Intent (An Activity with webview as Terms has URL Link).
As the word "Registering" and "Terms" are Web URLs, I tried something like below :
String mRegDesc = "By registering you agree to the " + "<a href=\""
+ Constant.URL + "/terms_and_conditions"
+ "\">Terms of Use</a> " + "and " + "<a href=\"" + Constant.URL
+ "/privacy" + "\">Privacy Policy</a> ";
txtByRegistering.setText(Html.fromHtml(mRegDesc));
txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
txtByRegistering.setSelected(true);
txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);
The above code works fine and it brings me to the browser when i click the word "Terms" But i wish to go to new Activity.
Finally,
I found the solution for that,
Here is the solution :
SpannableString SpanString = new SpannableString(
"By Registering you agree to the Terms of Use and Privacy Policy");
ClickableSpan teremsAndCondition = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
mIntent.putExtra("isTermsAndCondition", true);
startActivity(mIntent);
}
};
// Character starting from 32 - 45 is Terms and condition.
// Character starting from 49 - 63 is privacy policy.
ClickableSpan privacy = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
mIntent.putExtra("isPrivacyPolicy", true);
startActivity(mIntent);
}
};
SpanString.setSpan(teremsAndCondition, 32, 45, 0);
SpanString.setSpan(privacy, 49, 63, 0);
SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 32, 45, 0);
SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 49, 63, 0);
SpanString.setSpan(new UnderlineSpan(), 32, 45, 0);
SpanString.setSpan(new UnderlineSpan(), 49, 63, 0);
txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
txtByRegistering.setText(SpanString, BufferType.SPANNABLE);
txtByRegistering.setSelected(true);
thanks to Shayan pourvatan.
Let assume here is your complete string
By Signing up, I agree to Terms of Conditions & Privacy Policy
and string you want to make clickable is
Terms of Conditions and Privacy Policy
so, here is my trick.....
ClickableSpan terms = new ClickableSpan() {
#Override
public void onClick(View widget) {
new Utils(getActivity()).shortToast("Terms");
}
};
ClickableSpan privacy = new ClickableSpan() {
#Override
public void onClick(View widget) {
new Utils(getActivity()).shortToast("Privacy");
}
};
the main function for this
public void setClickableString(String wholeValue, TextView textView, final String[] clickableValue, ClickableSpan[] clickableSpans) {
SpannableString spannableString = new SpannableString(wholeValue);
for (int i = 0; i < clickableValue.length; i++) {
ClickableSpan clickableSpan = clickableSpans[i];
String link = clickableValue[i];
int startIndexOfLink = wholeValue.indexOf(link);
spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setHighlightColor(
Color.TRANSPARENT); // prevent TextView change background when highlight
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}
and here is the function calling
setClickableString(getString(R.string.terms_and_policy), tv_terms, new String[]{"Terms of Conditions", "Privacy Policy"}, new ClickableSpan[]{terms, privacy});
Use this one it works for me two click in single TextView
Step1-: Your text will be in SpannableString
SpannableString ss = new SpannableString("By Registering you agree to terms and condition and privacy policy");
Step2:-add click in ClickableSpan like this
ClickableSpan Registering = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent=new Intent(this,WebView_Activity.class);
startActivity(intent);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
ClickableSpan terms = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent=new Intent(this,WebView_Activity.class);
startActivity(intent);
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
Final step add your click on SpannableString with character starting and ending index like Registering word in start at 3rd position and end at 11 so add click for Registering word
ss.setSpan(Registering , 3, 11, 0);
same for term after this add your SpannableString on your TextView
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(ss, TextView.BufferType.SPANNABLE);
textview.setSelected(true);
I would suggest below code for clickable string in TextView it is dynamic.
Advantage of this code is if you have same String multiple times you can have click on both Strings. For example if you want to set click and String is Boy is playing cricket. Boy is playing football. Boy is two times both word will be clickable.
public void setClicksOnString(String completeString, List<String> stringsToClick, TextView textView) {
SpannableString spannableString = new SpannableString(completeString);
for (int m = 0; m < stringsToClick.size(); m++) {
Matcher matcher = Pattern.compile(stringsToClick.get(m)).matcher(spannableString);
while (matcher.find()) {
ClickableSpan stringClick = new ClickableSpan() {
#Override
public void onClick(View widget) {
//you compare the string and your click logics
}
};
spannableString.setSpan(stringClick, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textView.setHighlightColor(
Color.TRANSPARENT); // prevent TextView change background when highlight
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}
In addition, if you want to know in which text the user clicked dynamically, use below
ClickableSpan listener = new ClickableSpan() {
#Override
public void onClick(View textView) {
TextView tv = (TextView) textView;
Spanned s = (Spanned) tv.getText();
int start = s.getSpanStart(this);
int end = s.getSpanEnd(this);
String clickedText = s.toString().substring(start,end);
}
};
I have Button and its text, I retrive it from string.xml i.e.
I have declared a Button text in res/values in strings.xml like
<string name="Google">Google</string>
I removed its text color from blue to white but how do I remove its underline?
in my .java file I am using only setMovementMethod(LinkMovementMethod.getInstance()); to make the link clickabale... I didn't use Linkify, webview or anything like form.Html...
everything works fine. I just want to remove the underline below that "Google" text...
Is there any way to do this in the xml?
I even used android:autoLink="all". But when I used that, the text and button color changes and I dont want that.
Please Help.
TextView text=(TextView) findViewById(R.id.text);
String value = "<html> click to go <font color=#757b86><b>google</b></font> </html>";
Spannable spannedText = (Spannable)
Html.fromHtml(value);
text.setMovementMethod(LinkMovementMethod.getInstance());
Spannable processedText = removeUnderlines(spannedText);
text.setText(processedText);
here is your removeUnderlines()
public static Spannable removeUnderlines(Spannable p_Text) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
return p_Text;
}
also create class URLSpanNoUnderline.java
import co.questapp.quest.R;
import android.text.TextPaint;
import android.text.style.URLSpan;
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String p_Url) {
super(p_Url);
}
#Override
public void updateDrawState(TextPaint p_DrawState) {
super.updateDrawState(p_DrawState);
p_DrawState.setUnderlineText(false);
p_DrawState.setColor(R.color.info_text_color);
}
}
using this line you can also change the color of that text p_DrawState.setColor(R.color.info_text_color);
Kotlin has Textview extension for removing underline
fun TextView.removeLinksUnderline() {
val spannable = SpannableString(text)
for (urlSpan in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
spannable.setSpan(object : URLSpan(urlSpan.url) {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}, spannable.getSpanStart(urlSpan), spannable.getSpanEnd(urlSpan), 0)
}
text = spannable
}
Usage:
txtView.removeLinksUnderline()
after trying a lot finally i found the solution.
I removed the link from string.xml
and added this code in my java file.
Button btnGoogle = ( Button GoogleAlert.findViewById(
R.id.btnGoogle );
btnGoogle.setMovementMethod(
LinkMovementMethod.getInstance() );
btnGoogle.setOnClickListener( new OnClickListener()
{
#Override
public void onClick( View v )
{
Uri uri = Uri.parse(
"http://www.google.com" );
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
startActivity( intent );
buyAlert.dismiss();
}
} );
and it worked perfectly.
You add style=\"text-decoration:none\" to your <a> tag.
For more information on how your string resources should be formatted, check out the String Resources Documentation.
TextView text=(TextView) findViewById(R.id.text);
final String s = text.getText();
text.setText("");
text.setText(s);
that's all, folks ))