Set text above image in TextView using HTML - java

I have a list of Strings that I add to TextViews one by one. The text is html so I can add images within the text.
So my question is... How can I add an Image so that the text appears above it. currently while using HTML.ImageGetter class text is added below the image, not above it:
Here is my Html text to be shown:
myText += "<img src='titles.png'><span> text to be above image </span>";

Please try following code as example(Html text is changed):
myText += "<div style='position: relative;width: 100%;'> <img src='titles.png'><span style='position: absolute;top: 100px;left: 0;width: 100%;'> text to be above image </span></div>";
I referred http://css-tricks.com/text-blocks-over-image/ for checking for css properties.
Hope this is helpful for you.
Thanks & Regards,
Chanchal

you can add a simple alt="texthover" to the anchor tag and it will display the text in the alt in most browsers.
But that is not as good as creating an a.hover class that would display text or a div that has text.
Does that help?

Related

Android Java: Several ImageParser from html string

I want to display the content of news with Json. I tried to do Html.fromHTML() to display the style (bold, italic etc).
Then, I want to display image from html img in my textView.
I tried this image parser but this work only for the first image and the image pass over the text.
I need help for parse image from HTML or from json before Html.fromHTML()
This is my code: (without ImagePaser, this is in the link above)
URLImageParser p = new URLImageParser(description_projet, this);
Spanned htmlSpan = Html.fromHtml(json_data.getString("contenu"), p, null);
description_projet.setText(htmlSpan);
Thanks

Using HTML in Android Alert Dialog

I have some amount of informations to be displayed in Dialog Box. It comes like Title, then under it text; Title, then under it text. Like wise, there are 4 titles and 4 descriptions to be displayed. It should come like this
Title One
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
Title Two
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried.
TextView msg = new TextView(this);
msg.setText("<html><u>Message</u></html>")
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);
//rest of the code
However this trick didn't work. What happened is, all the HTML tags showed up as they are! And the text is not clear! Seems like it mixed with the background of the default colour of AlertBox, black. How can I solve this issue? Please help!
PS: Or am I using the wrong method? Wrong dialog box?
You will need to use Html.fromHtml() to use HTML tags in TextView as:
msg.setText(Html.fromHtml("<u>Message</u>"))
And you also see all HTML tags supported by TextView.
As it turns out, you don't actually need any extra TextViews to do this. Simply include the HTML in your alert's "setMessage()" call (which replaces the "setView()" call in your question) and pass it the html-formatted string. Be sure to only use <b>, <u>, and <i> in your formatting, though because those are the only tags it supports. If you're using a String resource for the text in your alert, call getResources().getText(R.id.yourHtmlString) rather than getResources().getString(R.id.yourHtmlString), though, or the tags will be completely stripped from the String.
If you want to add a link and make it clickable,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
If you need to add more complex HTML, with CSS and META, you can add a WebView to the dialog, like this:
String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",
null);
yourAlertDialog.setView(yourCustomWebView);
This way, you can display fully formatted HTML pages in your dialog.
Try this,
Font color,
String source = "<b><font color=#ff0000> Loading. Please wait..."
+ "</font></b>";
Font underline,
String source = <u>Message</u>
msg.setText(Html.fromHtml(source));
In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0) for compatibility with older versions.

How to add "..." ellipsis to lengthy text in a WebView?

I am using a WebView to align RTL text correctly.
Simply, I want to add "..." ellipsis when the text is lengthy.
Which is archived in a TextView using android:ellipsize="end"
Is there a way to achieve "..." ellipsis or to control the number of lines in a WebView?
Here is the code:
String header = "<html><head><meta http-equiv=\"Content-Type\" +" + "content=\"text/html; charset=UTF-8\" /></head>";
String dt = "<body dir=\"rtl\">" + o.get(p).getTitle() +"</body></html>";
webView.loadData(URLEncoder.encode(header + dt,"utf-8").replaceAll("\\+"," "), "text/html", "UTF-8");
You could parse the html and find out which is the last visible html element on the page. You could then use the following by appending the #ellipsis id to that element.
CSS Text Wrapping:
http://jsfiddle.net/6HcWM/
The problem will be finding the last visible html element as the screen size can vary, also the zoom level could presumably vary. I'm guessing that you'll need to append JavaScript to the html to discover this...
Maybe this would work:
How to tell if a DOM element is visible in the current viewport?
You have two simple options:
Use CSS and the text-overflow property on your HTML element: directions here.
If you need a bit better control where the ellipsis appears and have the means — include jQuery and a jQuery ellipsis plugin in your HTML via script tags: directions here.

TextView doesn't apply HTML styles using Html.fromHtml

I try to display an HTML text with its styles, but the text is shown without styling.
Is this normal behavior? It possible?
Is there a nice way to extract the styling from the HTML and apply it to my view?
In my code there are two numbers, one should be red and the other should be striped. In action, they both seems as simple text.
Here is my code:
String str = "<span>&nbsp <span dir=\"rtl\" style=\"text-align:right;color:#ff0000;\"> 20.00</span> <span dir=\"rtl\" style=\"text-align:right;text-decoration: line-through;\">26.99 </span></span>";
Spanned spanned = Html.fromHtml(str);
txtView.setText(spanned);
not all the html tags are supported (take a look here)
In my experience I have seen worked only div and p...

Change style of html links in TextViews after executing Html.fromHtml

I'm developing an android application. I retrieve some data that looks like this:
My Link to Google!</font></b>
I'm applying it to a TextView like this:
myTextView.setText(Html.fromHtml(myHtmlString));
The issue I encounter here is that Html.fromHtml seems to apply a general styling
to any and all links, which is to color them blue and underline them. I'd rather not have it do this, is there any simple solution to make it not stylize links(and therefore, I assume, "font color=whatever" would apply instead)? The behavior does not change if the HTML link tag is on the inside of font/style tags.
Use android:textColorLink attribute. I'm afraid it's the only way to change link color.
If you're sure that you have only one link in the text then you can do the following:
Spanned text = Html.fromHtml(myHtmlString);
ForegroundColorSpan spans[] = text.getSpans(0, text.length(),
ForegroundColorSpan.class);
if (spans.length > 0) {
myTextView.setLinkTextColor(spans[0].getForegroundColor());
}
myTextView.setText(text);

Categories

Resources