How to display HTML in TextView?
You need to use Html.fromHtml()
to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
This is what you should do in Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}
And in Kotlin:
textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(html)
}
Android best way to display html text
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);
}
}
How to display HTML img tag inside Android TextView
You should use WebView
instead of TextView
WebView = findViewById(R.id.WebView);
WebView.loadData(source, "text/html", "utf-8");
You will get the same output.
Related Topics
Gradle Error: Could Not Execute Build Using Gradle Distribution
How to Fix Attempt to Invoke Interface Method on a Null Object Reference
How to Start Android Application on Device Boot
How to Show the Number Keyboard on an Edittext in Android
Android Refresh a Fragment List from Its Parent Activity
How to Make Primary Key as Autoincrement for Room Persistence Lib
How to Prevent User Go Back to Protected Page After Logout
How to Access the Camera from Within a Webview
Configure Android Edittext to Allow Decimals and Negatives
Possible Unhandled Promise Rejection (Id:0) Error: Network Error in React Native
Java.Io.Filenotfoundexception Open Failed Eacces (Permission Denied) on Device
How to Change Listview Height Dynamically in Android
Changing Background Color of the Layout on a Button Click in Android
Android Studio - Xml Editor Autocomplete Not Working With Support Libraries
How to Check If a Value Exists Already in a Firebase Data Class Android