How to Display HTML in Textview along in Android?

There is some information displayed in HTML form on mobile applications. Android's inbuilt function doesn’t allow displaying HTML. when one needs to display rich formatted text in the app at times we can use HTML format. We are going to implement this project using the Kotlin language of mobile applications.







Below mentioned HTML Syntax.

 <!DOCTYPE html>

 <html

 <head>

    <title>

        HTML | <font> color Attribute

    </title>

 </head>

 <body>

    <font size="6" face="verdana" color="green">

        Candidroot solutions

    </font>

    </body>

 </html>

Here is the step by step Implementation process:

Step 1: Create a new project in Android  or you can use an already created project.  



Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside activity_main.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="10dp"
android:hint="Typing..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<Button
android:id="@+id/btnHtml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="10dp"
android:text="submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<TextView
android:id="@+id/txtHtml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnHtml" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Create an activity_main.kt file in your package folder .

 implement the same invoke the following code inside activity_main.kt file.

 import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;

public class MainActivity extends AppCompatActivity {
String htmlText = "<h2>What is Android?</h2>";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);

//create object of edtHtmlText
EditText edtHtmlText = findViewById(R.id.editText);

//create object of edtHtmlText
Button btnHtml = findViewById(R.id.btnHtml);

//create object of edtHtmlText
TextView txthtml = findViewById(R.id.txtHtml);

//set html text in edittext
txthtml.setText(HtmlCompat.fromHtml(htmlText, 0));


//set listener of button
btnHtml.setOnClickListener(
v -> {

//Get value from Edittext
htmlText = edtHtmlText.getText().toString();

//set html text in edittext
txthtml.setText(HtmlCompat.fromHtml(htmlText, 0));
}
);

}

}

 

Step 4: Output of above example




















Happy coding !




365Bloggy May 24, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive