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);
Related
I am currently trying to import selective headline from html content in my webview. I am looking at wide variety of options like json parsing or any hack will do. I was wondering if anyone has had experience with this or a brief idea on how to go about this?
Here's my example:
This is my html file content:
<div><h1><span class = "headline"> Some depressing title </span> <span class = "source" > ABCD </span> </h1> <br/> <span class = "body"> crappy body content which I do not need </span></div>
I just want to retrieve "headline" and "source" from this html in my webview, nothing else(not the body ). How do I go about defining a parameter to retrieve these? Any clues on how to do it?
Thanks!
Step 1: get the HTML source from your WebView - see this question. You basically create a JS interface that extracts your HTML source to a Java String.
Step 2: Use an HTML Parser (for example JSOUP) to parse the JAVA String into a format that you can handle easily.
Step 3: Use the parser to extract your relevant information. Here, you could use getElementsByTag('span') to get all your spans, then filter by class; or you could directly use getElementsByClass('healine') and getElementsByClass('source').
In general, you can retreive the HTML source and parse the DOM in all cases.
Edit: if you don't want to use a parser, you can extract your information by using searches on the HTML source string (finding the correct classes, then finding the indexes of '<' and '>' caracters to parse the information. This way is harder, less efficient, and less flexible, but it can be done.
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();
This is the html element in my Strings.xml file.
<string name="five">
<![CDATA[
<HTML>
<Body>
<p>.................Some Text.......................
<br><br>Feel free to contact us at<br> <font color="#add8e6">myemailid#gmail.com</font></p>
</body>
</HTML>
]]>
</string>
Now, when the user clicks on myemailid#gmail.com, I want the yahoo mail or gmail or which ever mail application is present in the user's phone to open up and the myemailid#gmail.com to fill up the TO : field. Is anything like this possible?
When you are setting the text you can use (assuming you have retrieved the string into a variable called text and are referenciing a TextView called textView)
textView.setText(Html.fromHtml(text));
When setting your layout you can also declare android:autolink="email" on the Views holding the five string.
The description of autolink says "Controls whether links such as urls and email addresses are automatically found and converted to clickable links." i.e. the text within the View is parsed and any (in this case) email addresses in the text are converted to clickable links.
In the case of android:autolink="email" your other Html tags in the text wouldn't be processed and would be display as they are above (rather than as a processed HTML document).
Whereas Html.fromHtml() will handle the HTML tags and deal with correctly formated tags.
Have a look at Androids Linkify class.
As an example, if you do something like:
Linkify.addLinks(myTextView, Linkify.EMAIL_ADDRESSES);
any email address in the textview will be clickable and when clicked, an appropriate dialog will be shown to the user.
Ofcourse, this is assuming that your string is actually displayed inside a TextView
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());