android kotlin - EditText border color programmatically

The code demonstrates how to programmatically set or change the border color of an EditText in Android. It defines two EditTexts in the activity_main.xml layout file and creates references to them in the MainActivity.kt file.

The code then uses an extension function called borderDrawable() to generate a drawable object with the specified border width, color, and background color. This drawable object is then set as the background of the EditText to give it a border.

Here is a breakdown of the code:

  • MainActivity.kt:

    • The borderDrawable() extension function takes three optional parameters:
      • width: The width of the border in pixels (default is 10).
      • color: The color of the border (default is black).
      • bgColor: The background color of the EditText (default is transparent).
    • The function creates a GradientDrawable object and sets its shape to RECTANGLE.
    • It then sets the stroke width, color, and background color of the GradientDrawable using the provided parameters or the default values.
    • Finally, it returns the GradientDrawable object.
  • activity_main.xml:

    • This file defines the layout of the activity.
    • It contains two EditText widgets with different IDs.

Summary

This code snippet provides a straightforward way to customize the appearance of EditTexts in an Android app by programmatically changing their border color. The use of extension functions makes the code more concise and reusable.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.text.InputFilter
import android.util.TypedValue
import android.widget.EditText


class MainActivity : Activity() {
    private lateinit var editText:EditText
    private lateinit var editText2:EditText

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

        // get the widgets reference from XML layout
        editText = findViewById(R.id.editText)
        editText2 = findViewById(R.id.editText2)

        // add specified color border to edit text programmatically
        editText.background = borderDrawable(
            2.dpToPixels(applicationContext), Color.BLUE
        )

        // add border and background color to edit text programmatically
        editText2.background = borderDrawable(
            width = 2.dpToPixels(applicationContext),
            color = Color.parseColor("#DA1884"),
            bgColor = Color.WHITE
        )
    }
}


// extension function to generate a drawable to show border
fun borderDrawable(
    width: Int = 10, // border width in pixels
    color: Int = Color.BLACK, // border color
    bgColor: Int = Color.TRANSPARENT // view background color
): Drawable {
    return GradientDrawable().apply {
        shape = GradientDrawable.RECTANGLE
        setStroke(width, color)
        setColor(bgColor)
    }
}


// extension function to convert dp to equivalent pixels
fun Int.dpToPixels(context: Context):Int = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, this.toFloat(),
    context.resources.displayMetrics
).toInt()
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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="32dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:inputType="text|textVisiblePassword"
        android:padding="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="12dp"
        android:inputType="text|textVisiblePassword"
        android:padding="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin - EditText border color programmatically
More android kotlin tutorials