Friday, 28 May 2021

How to set HTML tags on TextView. How to set HTML tags in TextView How to show TextView as HTML.. How to work TextView as HTML ags

Hello Everyone

First we create a xml file for TextView 

demo.xml file name

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:id="@+id/textHtmlTags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="demo html tags"
            android:textColor="@color/darkGrey"
            android:textSize="@dimen/_12sdp" />
</androidx.constraintlayout.widget.ConstraintLayout>

and now in Kotlin file 

DemoActivity.kt

package com.akharipoint.htmldemo

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.akharipoint.htmldemo.R

class DemoActivity : AppCompatActivity() {

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.demo)
val textHtmlTags:TextView=findViewById(R.id.textHtmlTags)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            textHtmlTags.text = Html.fromHtml(
                        bodyMessage,
                        Html.FROM_HTML_MODE_COMPACT
                     )
        } else {
            textHtmlTags.text =Html.fromHtml(bodyMessage)
        }
    }

}

Thanks 

AkhariPoint Teams