How to format text in string displayed in TextView - android

I'm trying to format text in string, which is displyed in TextView.
<string name="register"> <p align="center"><u><b><i>Something</i></b></u></p>
<p align="right">Blablabla</p>
Underline, bolt and italics are working, but new paragraph and align isn't. Am I doing something wrong? Is there some other tag for end of a paragraph/new line?

Refer here http://bigknol.com/android-supported-html-tags-textview-string/
Not all html tags are supported in android. As per the link only the following tags are supported:
<big></big>
<small></small>
<bold></bold>
<strike></strike>
<a href=””></a>
<sup></sup>
<sub></sub>
<u></u>
<i></i>
<tt></tt>
Also refer here Which HTML tags are supported by Android TextView?
Another reference is here http://javatechig.com/android/display-html-in-android-textview

You can use
myTextView.setText(Html.fromHtml("<h2>Heading</h2><br><p>data about something</p>"));

Related

How to make all kind of links clickable in Android textview from Rich text/html string

I have some rich texts returned from api which may have phone-numbers, raw text-links, hyperlinks email. I want to make everything clickable using Android textview.
for an example :
<p>Hello <i>Mr.Foo</i>.<br>Thank you for visiting us.<br>
<br>For more details, click this link: My Web Link
<br> You can also visit another website: www.anotherweb.com <br><br>
<b>For any enquiries:<br>
<br>Mr.Boo</b>
<br>Contact: (65) 1020 2401 / +6512345678<br>
Email: booking#example.com<br><br>
</p>
As you can see there are
1 hyperlink with <a href=""> tag,
1 raw text web link ( www.anotherweb.com )
1 email address without <a href="mailto:.. tag)
2 phone numbers
I tried to use following (along with Html.fromHtml() )
Textview.setMovementMethod(LinkMovementMethod.getInstance()) :
Hyperlinks become clickable but email, phone number or raw text links unable to click.
Used autoLink="all" in Textview :
Everything else worked except hyperlinks( the one with <a href=""> become not clickable)
Used Linkify.addLinks(textview, Linkify.ALL) :
Again unable to click on hyperlinks but others worked fine.
I tried also combination of these but couldn't make everything clickable at once. Is there any other way exist?

Format a string with html elements in react native

I am trying to create a component to display some generic product info. The string is formatted by a CRM so it has HTML elements in it "<BR>, <b> </b> etc.
eg:
"<BR>Łosoś to<b> szlachetna ryba,</b> można z niej przyrządzać przepyszne dania główne, genialne przystawki, a także niezwykle smaczne pasztety i farsze. Doskonale nadaje się do<b> pieczenia, smażenia, duszenia i grillowania. <BR><BR>Trym D:</b> Usunięty kręgosłup, ości brzuszne, tylne płetwy, kość obojczyka, tłuszcz na płetwie brzusznej usunięty częściowo, usunięte ości, ścięty ogon, usunięta otrzewna.<BR>"
I want to format the string so it renders properly in react native.
I tried using ramda's replace and it worked for adding new lines /n however I do not think this will work for making the text bold.
is there some library that will format strings with html elements into a react native friendly format?

How to get content of the tag using JSOUP

I have an HTML code like this:
<div class="newPrice">
<ins content="1857.00">1.857,00 <span content="TRY">TL</span></ins><span class="kdv">KDV <br>DAHİL</span>
</div>
I want to get 1857.00 (not 1.857,00). How can I get that?
I tried
Elements den = doc.select(":containsOwn("1.857,00")");
and it returns
<ins content="1857.00">1.857,00 <span content="TRY">TL</span></ins>
I couldn't go any further
Thanks in advance.
I solved the problem. If I say:
String price = den.attr("content");
it returns 1857.00
The ideal approach is this way:
Element ele = doc.select("div.newPrice ins").first();
String price = ele.attr("content");
System.out.println(price);
If we use .text() it will written all the text present on the CSS path,
When it comes of attributes we have to use .attr() to get the desired values.

Html.fromHtml on Nougat doesn't call custom HtmlHandler

I have a custom TextView, to show html text. For pre-Nougat devices it works. As you already know on Nougat, fromHtml is deprecated and it needs a flag..so my code is like this
Spannable s = getRichText(text);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
super.setText(Html.fromHtml(s.toString(), Html.FROM_HTML_MODE_COMPACT, this, new HtmlHandler(getContext())), BufferType.SPANNABLE);
} else {
super.setText(Html.fromHtml(s.toString(), this, new HtmlHandler(getContext())), BufferType.SPANNABLE);
}
The problem it that, the HtmlHandler class is never get called. (I have already tried all the flags).
On HtmlHandler I handle tags and styles, eg background-color, color et cetera. I have implemented to get colors from rgb, rgba, hls etc. But on Nougat it accepts only colors with hex, because on Nougat, fromHTML can "read" colors and show them. Why is this happening? How can I keep my way for the colors? If you didn't understand something, or need more details, let me know.
The html I use for testing is
<p><strong>Server</strong><u> message</u><strong><u>!!!</strong></u> <span style="background-color: rgb(255,0,0);">Not working on Nugat</span></p>
Html.fromHtml() will only invoke your TagHandler for HTML tags that fromHtml() does not recognize. In your sample HTML, you have:
<p>
<strong>
<u>
<span>
and in your first comment, you also mention div.
Of those, fromHtml() has handled <p>, <strong>, and <u> since at least 2010, if not earlier. fromHtml() in Android 6.0 also handles <div> (see lines 488-489 in the source), and I forget how back that support goes. Your TagHandler would not be called for any of these tags, and that behavior is not especially new.
Android 7.0 added support for <span> (see lines 804-805 from the 7.1 source), and so code that expected a TagHandler to be invoked for <span> would behave differently between Android 7.0 and previous versions.
In general, the list of supported tags is undocumented. Google is welcome to change the roster of supported tags at any point.
Your options are:
Live with it
Grab the source to some Html.java that you like, refactor it into your own package, and use that copy, modifying it as you see fit
Find some other HTML-to-Spannable source code that you like better

Android TextView: Html.fromHtml() - Valid URL / Link in an <a> link tag rules?

Are there any rules around (or just the regexp) that explain which URL's a Html.fromHtml(...) call will linkify / accept when placed in an < a > tag?
I ask as I'm wasting hours trying to work out what causes some links to be clickable, while others are unclickable (rendered as plain text) by the call e.g
If you stick ALL of the following in a:
TextView aboutText = (TextView) v.findViewById(R.id.about_text);
aboutText.setText(Html.fromHtml(.....
call it accepts / will render as a link:
<p> ... the Developer.android.com samples, </p>
<p> - https://en.wikipedia.org/wiki/Android_(operating_system)</p>
But it won't accept the Wikipedia link presented as:
<p> - <a href="https://en.wikipedia.org/wiki/Android_(operating_system)">Wikipedia:
Android</a></p>
The textview has the android:autoLink="web" attribute set.

Categories

Resources