How to show Text and Images in a TextView - java

This is the json response below from ifeng api.
Please help me how to show text object in a TextView including images in the middle.

Use webwiev and load following json response(text witch are in html form) on webview.

Try This
First pars that data ,then get text from text param
After getting the HTML text You need to use Html.fromHtml()
If your you are targeting < Android Nougat
yourTextView.setText(Html.fromHtml("PUT YOUR HTML CODE HERE"));
If your you are targeting >= Android Nougat
yourTextView.setText(Html.fromHtml("PUT YOUR HTML CODE HERE", Html.FROM_HTML_MODE_COMPACT));
Note:-
To distinguish between Android versions use Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
Happy Coding!!

Try using TextView.setText(Html.fromHtml(...)), then using html tags in ..., but you need to implement Html.ImageGetter
Reference: https://stackoverflow.com/a/7442725/1636610

you can show image in textview as drawable like below -
tvTitle.setText("Hi");
tvTitle.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);//it will show icon image on left side of the text on tvTitle text view.

Related

How to show JSON volley request reponse's object (in HTML) form in textView?

I have a JSON response in which object values are in HTML form. In which i have a Paragraph, a img src and video src link. How can i parse or show the image and video onto the Activity. Any help would be appreciated.
Response I'm receiving from server(only showing one object here)
{
"description":"<p>The Sarawak Ministry of Modernisation of Agriculture</p>\n
<p><video src=\"https://myurl.biz/media/media/Nokia_X3_sample_video.3gp.3gp\" autoplay=\"autoplay\" controls=\"controls\" width=\"300\" height=\"150\">
<span id=\"mce_marker\" data-mce-type=\"bookmark\">â</span></video>
</p>\n<p><img src=\"https://myurl.biz/media/media/bird_SJhLHPo.jpg\" width=\"547\" height=\"264\" /></p>"
}
I have tried :
txtDetails.setText( Html.fromHtml(extras.getString("details"));
Actual Result I'm receiving in my text view:
The Sarawak Ministry of Modernisation of Agriculture,
​
What you're trying to do is impossible. You can't display an image/video in a TextView (this is not a browser). First, you need to parse the HTML. Then, once you have the components you can plug them in separate views. For example,
You parse the HTML (there are a lot of frameworks for that)
Get the image URL link and use Picasso (popular framework for displaying images from URLs) to display it on your Activity.
Get the paragraph and display it in a TextView
etc..

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.

Set text above image in TextView using HTML

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?

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