I have string with html tags
, links
, image links
so what is the best way to display this string? I know that people using Webview
for that but maybe there is a way to do it in textview without putting too much work? Because with WebView
comes different problems for example if you want to change text color you need to add extra style to that string. I'm interested in ways to make links clickable and displaying Images in the same textview.
Asked
Active
Viewed 1.2k times
3

LeTadas
- 3,436
- 9
- 33
- 65
-
What is the complete list of tags that you will be using in the HTML? – CommonsWare Jun 03 '18 at 18:41
-
2Possible duplicate of [How to display HTML in TextView?](https://stackoverflow.com/questions/2116162/how-to-display-html-in-textview) – Yassin Yassin Jun 03 '18 at 19:14
-
Its not duplicate you see I know how to display html in textview but how about links and making them clickable using textview and as well displaying images from link inside this textview – LeTadas Jun 03 '18 at 19:51
3 Answers
7
Try using this
:-
Html.fromHtml("your html code");
Example
:-
txtvw.setText(Html.fromHtml("<p align=right> <b> "
+ "Hi!" + " <br/> <font size=6>"
+ " How are you "+"</font> <br/>"
+ "I am fine" + " </b> </p>"));
Output
:-
Hi
How are you
I am fine
**Full Code With Image And Hyperlink**
:-
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends Activity {
String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='https://stackoverflow.com/'>Stack Overflow</a></b>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView htmlTextView = new TextView(this);
setContentView(htmlTextView);
htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){
@Override
public Drawable getDrawable(String source) {
Drawable drawable;
int dourceId =
getApplicationContext()
.getResources()
.getIdentifier(source, "drawable", getPackageName());
drawable =
getApplicationContext()
.getResources()
.getDrawable(dourceId);
drawable.setBounds(
0,
0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
}, null));
htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
To support all API use this function
:-
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
}
else {
return Html.fromHtml(html);
}
}

user2342558
- 5,567
- 5
- 33
- 54

Shashank Mishra
- 542
- 4
- 12
-
-
Nice! one more thing from I guess 27 api fromHtml is deprecated would this approach work with 27 and higher api's? – LeTadas Jun 03 '18 at 20:13
0
The way it only worked for me was:
class HelpActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.help_activity)
val txtView: TextView = findViewById(R.id.help_textView)
val str = resources.getString(R.string.help_textview)
txtView.text = Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT)
}
}
and the string resource uses a CDATA wrapper:
]]>

rwst
- 2,515
- 2
- 30
- 36
0
In addition to what previously written, I find that using Html.fromHtml and HtmlCompat do not rendered ordered list (i.e. with numeric header)
<ol><li>...</li></ol>
I find this library that render that tag and it works for me: HtmlTextView

android_dev71
- 517
- 5
- 7