The html and css content must be displayed perfectly within the app. All types of HTML content will be shown perfectly in webview. We can also show textviews, but html and css won’t show perfectly, so babyviews would be better.
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
WebView webView = findViewById(R.id.webView);
// enable js if we needed
webView.getSettings().setJavaScriptEnabled(true);
String question = "<h3>Question Title</h3>";
String option1 = "<p>1️⃣ <b>ans 1</b></p>";
String option2 = "<p>2️⃣ <b>ans 2</b></p>";
String option3 = "<p>3️⃣ <b>ans 3</b></p>";
String option4 = "<p>4️⃣ <b>ans 4</b></p>";
String htmlContent = "<html><body>" + question + option1 + option2 + option3 + option4 + "</body></html>";
webView.loadData(htmlContent, "text/html", "UTF-8");
Why is this the best method for showing mcq and 4 type answer into wenview in android studio?
- Using Only One WebView – Faster & lightweight.
- Properly formatted text – Supports <h1>, <p>, <sup>, emojis, and symbols.
- Easy to Manage – No need for multiple TextViews.
If you want to display HTML content properly into a TextView without using a WebView, there are some great libraries available
HtmlTextView (Best for Lightweight Apps)
GitHub: https://github.com/SufficientlySecure/html-textview
Features: Supports bold, italic, underline, links, images, lists, and more.
Works inside TextView without WebView.
Fast and lightweight
dependencies {
implementation 'org.sufficientlysecure:html-textview:4.0'
}
XML Code:
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="@+id/htmlTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Java Code:
HtmlTextView htmlTextView = findViewById(R.id.htmlTextView);
htmlTextView.setHtml("<h1>THBD</h1> <p>Tech<b>Harvest BD</b>!</p>");
Now we HTML content will display perfectly inside TextView or WebView.
