Sunday, August 30, 2020

android kotlin - EditText border color programmatically

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.TypedValue
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 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FEFEFA"
    tools:context=".MainActivity">

    <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>