A number of discussions on here going back years related to getting hyperlinks to work in a TextView. The conclusion is that autoLink works for parsing out URLs that are simply embedded in the text, e.g., "go to www.google.com". Then there is
setMovememtMethod(LinkMovementMethod.getInstance());
that will cause actual HTML tags to work, e.g. go to Google. However, using the latter causes autoLink to not work in the same view.
My issue is that I am displaying text that is supplied by a user database, so I have no control over the formatting. In some cases, this text has plaintext links while in others it is entered as HTML tags. Is there any way to get both types of links to work at the same time?
Both plain text links & links with HTML tags will work with the below code
TexView in xml
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textColorLink="#06b" />
Here in activity.java
String text = "this is the link with anchor tag Google. here is the plain text link http://www.google.com";
TextView textView = (TextView) findViewById(R.id.txt_view);
textView.setText(getParsedLinks(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
instead of using android:autoLink="all" in xml or Linkify.addLinks(textView, Linkify.ALL) use Linkify.addLinks to SpannableString as in below method
SpannableString getParsedLinks(String txt){
Spanned span = Html.fromHtml(txt);
URLSpan[] urlSpans = span.getSpans(0, span.length(), URLSpan.class);
SpannableString s = new SpannableString(span);
Linkify.addLinks(s, Linkify.ALL);
for (URLSpan urlSpan : urlSpans) {
s.setSpan(urlSpan, span.getSpanStart(urlSpan), span.getSpanEnd(urlSpan), 0);
}
return s;
}
Related
I thoroughly searched on SO but didnt get answer of my question.
I want to set a paragraph, I will set it in XML using
The text contains Title and Steps and regular text. I want to make the Title and Steps in bold and rest in normal text.
I can do this by using different 's but how can I do it in the same TextView.
I mean using the same TextView how can I set different attributes for different sentences?
Use a Spannable String
TextView tv = (TextView) findViewById(R.id.tv);
String steps = "Hello Everyone";
String title="Bold Please!";
SpannableString ss1= new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
tv.append(ss1);
tv.append("\n");
tv.append(steps);
For more styling check the link # http://blog.stylingandroid.com/archives/177
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
And output
You can format it like you would in HTML: let's call this custom_text
<b>Your title here</b>
This is the non-bolded stuff.
And then load the text using the Html class:
mTextView.setText(Html.fromHtml(getString(R.string.custom_text)));
That will create a spannable string and set it on the TextView.
please put this string in to res->string.xml
<string name="your_html">
<![CDATA[p><b>This is bold text</b> rementing is simple text
]]>
</string>
Now you can used whenever you have to require this thing.
tv.setText(Html.fromHtml(getString(R.string.your_html)));
It is bestway and work charm.
TextViews support SpannableStrings. You can either make your custom String or format your string in html and then set it with tv.setText(Html.fromHtml(yourString));
kotlin Solution :
For me, the issue was I had a text set in XML through data binding. I had to remove that. then this started taking effect
val spannableStringBuilder = SpannableStringBuilder(headerText)
spannableStringBuilder.setSpan(StyleSpan(Typeface.BOLD), 0, headerText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableStringBuilder.append(" ")
spannableStringBuilder.append(subText)
textView.text = spannableStringBuilder
The best way to do this is in the Strings resources.
<string name="sample_string"><![CDATA[<b>Title</b>StackOverflow]]></string>
Notice, the text between bold tag (<b> </b>) will appear in bold. Similarly, you can set other styles. For eg. <i> for italics and <u> for underline.
Hope this helps, good luck!
Instead, use this :
First, declare textView
TextView tv1 = (TextView) getActivity().findViewById(R.id.t1);
Then set it's text to the string you want
tv1.setText(Html.fromHtml(getString(R.string.my_text)));
Lastly, use set the typeface for your textView
tv1.setTypeface(null, Typeface.BOLD);
And, you are done.
I have got the following issue. I have a String which I want to display in a TextView. Because the String can be HTML formatted I am using a Spanned like so:
TextView tv = (TextView)findViewById(R.id.Description);
Spanned myDescription = Html.fromHtml(myDescriptionOriginal, null, null);
tv.setText(myDescription, TextView.BufferType.SPANNABLE);
The "problem" I now have is that when the myDescriptionOriginal is not HTML formatted, the fromHtml(...) removes all newlines (and presumably also things like tabs).
Is there a general solution (perhaps as part of the Spanned/Spannable/Html classes, with which I am not too familiar) that solves this problem or is this something I have to write myself (I could for instance check if the myDescriptionOriginal contains HTML tags and if it doesn't I don't use the fromHtml)
I am trying to get a textview to process a hyperlink as well as phone numbers. Say my text is:
"555-555-555, www.google.com, Google!"
If I run Html.fromHtml() on this string, then the TextView shows Google! correctly as a clickable link but not the other two.
If I run Linkify.addLinks(TextView, Linkify.All) on the TextView, then the first two are correctly recognized as a phone number and url, but the html is not processed in the last one.
If I run both of them, then either one or the other is honored, but not both at the same time. (Html.fromHtml will remove the html tags there, but it won't be a link if linkify is called after)
Any ideas on how to get both of these functions to work simultaneously? So all the links are processed correctly? Thanks!
Edit: Also, the text is changed dynamically so I'm not sure how I would be able to go about setting up a Linkify pattern for that.
It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.
Use this code to get it work:
public static Spannable linkifyHtml(String html, int linkifyMask) {
Spanned text = Html.fromHtml(html);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, linkifyMask);
for (URLSpan span : currentSpans) {
int end = text.getSpanEnd(span);
int start = text.getSpanStart(span);
buffer.setSpan(span, start, end, 0);
}
return buffer;
}
try to set movement method on your textview instead of using Linkify:
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your TextView's xml layout, you should add the following:
android:autoLink="all"
android:linksClickable="true"
Then you should remove your Linkify code in Java.
It works somehow, but I dont know why. I added a question to see if someone can explain the behavior: Using Linkify.addLinks combine with Html.fromHtml
Is there any control in android which we can use as a rich text box which can handle formatting of characters such as bold, italic etc. and use fonts and color ?
If anyone having any link or source related to the above information please share it with me.
SpannableString class or HTML.fromHtml() allows you to manipulate different styles in actual string. See the links below:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)
Sample SpannableString and TextView implementation:
TextView tv = new TextView;
String text = "String String String String body";
SpannableString spannableStr = new SpannableString(text);
spannableStr.setSpan(new StyleSpan(Typeface.BOLD), 0 , 10, 0);
tv.setText(spannableStr);
WebView is the closest that I can think of, but it is super duper heavy unless you want the entire screen to be a webview.
i have a textview whose text i am setting as a big string.
this string has several parts like telphone no, mail , browsing address.
i have found how to use the browsing address in a span to open up the link in a browser?
here is the code
TextView tv = (TextView) findViewById(R.id.infotest);
SpannableString ss = new SpannableString(getResources().getString(R.string.clientaddress));
ss.setSpan(new URLSpan("http://"+getResources().getString(R.string.clientaddress)), 0, 23,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
how do i do the same for telephone text.
i mean how do i span the telephone text to call a the number when the user presses that specific part of the textview string.
You have to set it from the xml file.
For example:
android:id="#+id/mobile_number"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:autoLink="phone"
Available values for the autoLink field is: web, email, phone, map, all
Can you use Linkify?
Linkify.addLinks(tv,Linkify.PHONE_NUMBERS);
ETA or try using tel:// instead of http?