Thursday, February 27, 2020

android kotlin - Underline TextView text programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.widget.*
import androidx.core.text.HtmlCompat


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        // get the widgets reference from XML layout
        val buttonPaint = findViewById<Button>(R.id.buttonPaint)
        val buttonSpan = findViewById<Button>(R.id.buttonSpan)
        val buttonHtml = findViewById<Button>(R.id.buttonHtml)
        val buttonHtmlPart = findViewById<Button>(R.id.buttonHtmlPart)
        val textView = findViewById<TextView>(R.id.textView)

        
        // text to show on text view
        val string = "Lorem Ipsum is simply dummy text of the printing" +
                " and typesetting industry. Lorem Ipsum has been the" +
                " industry's standard dummy text ever since the 1500s," +
                " when an unknown printer took a galley of type" +
                " and scrambled it to make a type specimen book."

        textView.text = string


        // underline text view part of text by span
        buttonSpan.setOnClickListener {
            val text = SpannableString(string)
            text.setSpan(
                UnderlineSpan(),
                50, // start
                150, // end
                0 // flags
            )
            textView.text = text
        }


        // underline text view full text using html
        buttonHtml.setOnClickListener {
            val htmlString = "<u>$string</>"
            textView.text = HtmlCompat.fromHtml(
                htmlString,
                HtmlCompat.FROM_HTML_MODE_LEGACY
            )
        }


        // underline text view part of text using html
        buttonHtmlPart.setOnClickListener {
            val builder = StringBuilder(string)
                .insert(125, "<u>")
            builder.insert(175+3,"</u>")
            textView.text = HtmlCompat.fromHtml(
                builder.toString(),
                HtmlCompat.FROM_HTML_MODE_LEGACY
            )
        }


        // underline text view full text by paint
        buttonPaint.setOnClickListener {
            textView.paintFlags = textView.paintFlags or
                    Paint.UNDERLINE_TEXT_FLAG

            // you need to remove underline paint flag
            // to implement another way
            // to remove all paint flags
            //textView.paintFlags = 0
        }
    }
}
activity_main.xml

<?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/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/buttonSpan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnderLine Text Part (Span)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonHtml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="UnderLine All Text (HTML)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonSpan" />

    <Button
        android:id="@+id/buttonHtmlPart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="UnderLine Text Part (HTML)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonHtml" />

    <Button
        android:id="@+id/buttonPaint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="UnderLine All Text (Paint)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonHtmlPart" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textColor="#2A52BE"
        android:fontFamily="sans-serif"
        android:textSize="28sp"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonPaint"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>