i'm a beginner user of jsoup, i would like to use it with android sdk.
I'm doing some testing, i would like to know how i can get a specific text in a html page.
The html code is the following:
<span class="time_rtq_ticker"><span id="yfs_l10_eurusd=x">1,3809</span></span>
I would like to get that number "1,3809" and my query using jsoup is something like this:
Element euro = doc.select("span[id~=yfs_l10_eurusd=x]").first();
value = euro.toString();
Then i display it in a textview, in this way:
tv = (TextView) findViewById(R.id.textView2);
tv.setText(value);
The problem is that my app show the full html code, that is:
<span id="yfs_l10_eurusd=x">1,3809</span>
How can i get only the number? Why i also get the html code?
Use the Element.text() method
Element euro = doc.select("span[id~=yfs_l10_eurusd=x]").first();
value = euro.text();
Related
I'm using Wordpress API to get posts in my app. I'm trying to add new line while writing a post. However when I ,for example, add post like this :
THIS IS /n a POST
In my app I can see /n in the textView exactly as I put it above. The same thing is happening when I try to use <br> tag.
Setting post in textView:
String content = String.valueOf(HTML.fromHTML(p.excerpt));
vItem.shortContent.setText(content);
Why is that ?
EDIT!!!
It's working after adding :
String content = p.excerpt.replace("\\n","<br>);
vItem.short_content.setText(Html.fromHtml(content));
Thanks!
Supposing p.excerpt is an HTML String, use
vItem.shortContent.setText(Html.fromHtml(p.excerpt));
instead.
You are seeing the new lines because String.valueOf(Object) calls toString() from the param object. Since Html.fromHtml() returns an Spannable, your code is doing a spannable.toString() so its returning the literal text.
you can also put the html formated text in the string resources directory as follows
<string name="helloworld">
<![CDATA[
This is now formatted:<br />
<strong>Hello world</strong> - this should work fine
]]>
</string>
you can then call the string in your activity as follows
txtView = findViewById(R.id.mytext);
String formatedstring = getString(R.string.helloworld);
Spanned txt = Html.fromHtml(formatedstring);
txtView.setText(txt);
I know that I can use the default Html.fromHtml(string) but it highlights links by default. Is there a way to prevent that behavior?
P.S.
I'm trying to feed it straight to TextView using .setText() without saving it to String. I want to keep all the formatting except links.
If you only need text you can fetch it with:
String string = Html.fromHtml(string).toString();
Edit:
Since you want to remove only the links, you can use String.replaceAll before parsing the html:
// Remove <a href*>
html = html.replaceAll("<a href.*?>", "");
// Remove </a>
html = html.replaceAll("</a>", "");
textView.setText(Html.fromHtml(html));
Android's TextView class can display formatted text via HTML.fromHtml() as explained for example here: HTML tags in string for TextView
The TextView class can only deal with a small subset of HTML, but I do not know which tags and attributes are supported and which are not. The summary given here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html does not seem to be correct. E.g. <div align="..."> does NOT work for me using Android 2.2
Looked it up for everyone searching for it.
Date: July 2017
Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java
Html.fromHtml supports:
p
ul
li
div
span
strong
b
em
cite
dfn
i
big
small
font
blockquote
tt
a
u
del
s
strike
sup
sub
h1
h2
h3
h4
h5
h6
img
br
I noticed that this article:
https://web.archive.org/web/20171118200650/http://daniel-codes.blogspot.com/2011/04/html-in-textviews.html
lists <div> as being supported by Html.fromHtml(), but it doesn't show support for the "align" attribute.
(Other supported attributes are shown for tags on that page.)
The author says he constructed the reference by looking at code in the git repositories for Android.
Edit:
Over time, it appears the list of supported tags has changed. See this later post for example: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ .
Based on both those articles, I'd suggest that examining the source code seems to be the most reliable way to get the recent information.
The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.
//in string.xml file
<string name="welcome_text"><![CDATA[<b>Welcome,</b> to the forthetyroprogrammers blog Logged in as:]]> %1$s.</string>
Java code
String welcomStr=String.format(getString(R.string.welcome_text),username);
tvWelcomeUser.setText(Html.fromHtml(welcomStr));
CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.
Output:
Welcome, to your favorite music app store. Logged in as: username
Since it is constantly being updated, the best way to track which HTML tags are supported in Android is to check the source code of Html.java
I'm trying to add a link to a Twitter profile in an about box. 'Regular' links such as email address and web address are handled by
android:autoLink="email|web"
in about.xml, but for a Twitter profile page I need to use html code in my strings.xml. I've tried:
<string name="twitter">Follow us on <a href=\"http://www.twitter.com/mytwitterprofile">Twitter: #mytwitterprofile</a></string>
which renders html markup on the about box.
I've also tried:
<string name="twitter">Follow us on Twitter: #mytwitterprofile</string>
which display the text "Follow us on Twitter: #mytwitterprofile", but it is not a hyper-link.
How do I do this seemingly simple task!?
Cheers,
Barry
The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:
<string name="sampleText">Sample text <![CDATA[link1]]></string>
And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
The simple answer is that the TextView does not support <a> tags. AFAIK, it only supports basic formatting such as <b>, <i> and <u>. However, if you supply android:autoLink="web", the following string:
<string name="twitter">Follow us at twitter.com/mytwitterprofile</string>
Will turn twitter.com/mytwitterprofile into a proper link (when set via XML like android:text="#string/twitter"; if you want to set it from code, you'll need the Html.fromHtml method someone else posted in an answer).
I'm not too sure how to link using 'strings', but you could set text of the EditText or TextView using fromHtml...
TextView text = (TextView) findViewById(R.id.text);
text.setText(Html.fromHtml("Google Link!"));
text.setMovementMethod(LinkMovementMethod.getInstance());
I have parse the url and i get the html contents i want to show that content as a web page then what can i do to show that html contents. I am using textView then it shows same html content with tags.
I have following html content which i want to display in webView. It displays only
**// Please** but its original output is not this.
<br /><br />Read the handouts please for tomorrow.<br /><br /><!--
homework help
homework help
help with homework
homework assignments
elementary school
high school
middle school
// --><font color="#60c000" size="4"><strong>Please!</strong></font>
its original output is
Read the handouts please for tomorrow.
Please!
I have other html also where there is a tag of img but this shows nothing
Please Suggest me how to solve this problem
If you want to use the html coding and view just the content without the html tag and that too in textview you have to use this syntax in the java file :
TextView textView = (TextViewPlus) findViewById(R.id.textViewId);
textView.setText(Html.fromHtml(getString(R.string.htmlString)));
the above line will fetch the string content in the string.xml file and in string.xml file you can give the content in html format , something like this :
<string name="htmlString"><![CDATA[
<div>
<p>hello this can be you any content and even you can use the other html tags.</p>
</div>
]]></string>
let me remind you the content has to be inclosed between: <[CDATA[......your html code goes here........]]>
There are two ways. Either go for a webview which can encode HTML tags automatically or if you want to use Textview you can use Html.fromHtml() method available on Android....Then set the Text to your Textview. Something like this
Textview.setText(Html.fromHtml(source));
or Even like this
Spanned t=Html.fromHtml("HTML String that needs to be displayed");
yourtextView.setText(t);