Android Kotlin:TextView add border programmatically

Adding Border to TextView in Android with Kotlin

This code snippet demonstrates how to programmatically add a border to a TextView in an Android application written in Kotlin. It achieves this by creating a custom extension function for the TextView class.

Code Breakdown

  1. MainActivity.kt:

    • This file defines the main activity of the application.
    • In the onCreate method, it sets the content view of the activity and then calls the addBorder extension function on the textView to add a red border with a width of 20dp.
  2. addBorder Extension Function:

    • This function takes two optional parameters: color (defaults to gray) and width (defaults to 10dp) for customizing the border.
    • It creates a ShapeDrawable object and sets its shape to a RectShape.
    • Inside the paint property of the drawable, it configures the border by setting the color, width, and style (stroke) of the paint object.
    • Finally, it sets the background of the TextView to the created ShapeDrawable with the border.
  3. activity_main.xml:

    • This file defines the layout of the main activity.
    • It uses a ConstraintLayout as the root layout and contains a single TextView element with the ID textView.

Summary

This code provides a clean and reusable way to add borders to TextViews in an Android application. The addBorder extension function simplifies the process by encapsulating the border creation logic and offering customization options for color and width. This approach is flexible and can be applied to any TextView in your project.


MainActivity.kt

package com.example.jetpack

import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // add a border around text view
        textView.addBorder(Color.RED, 20F)
    }
}


// extension function to add border to text view programmatically
fun TextView.addBorder(
    color : Int = Color.GRAY,
    width : Float = 10F
){
    // initialize a shape drawable
    val drawable = ShapeDrawable().apply {
        // specify the shape of shape drawable
        shape = RectShape()

        paint.apply {
            // specify the border color of shape
            this.color = color

            // set the border width
            strokeWidth = width

            // specify the style is a Stroke
            style = Paint.Style.STROKE
        }
    }

    // finally, add the shape drawable background to text view
    background = drawable
}
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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEAE0"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="25dp"
        android:text="Sample TextView"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials