Create EditText in Kotlin Programmatically

In this short code example in Kotlin I am going to share with you how to create an EditText programmatically and also how to:

  • Add the created EditText in a ConstraintLayout
  • Set hint text for the TextView
  • Set text color
  • Set EditText backgroundColor

Layout XML file for the EditText UI component

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="appsdeveloperblog.kotlin.codeexamples.ButtonExample">

</android.support.constraint.ConstraintLayout>

Create EditText in Kotlin Programmatically. Source code.

package appsdeveloperblog.kotlin.codeexamples

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintSet
import android.widget.EditText

class EditTextExample : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_text_example)
        val constraintLayout = findViewById(R.id.constraintLayout) as ConstraintLayout

        val editText = EditText(this)
        editText.hint="Type here"
        editText.setTextColor(Color.BLUE)
        editText.setBackgroundColor(Color.YELLOW)
        editText.layoutParams = ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.MATCH_PARENT)

        constraintLayout.addView(editText)

        var constraintSet = ConstraintSet()
        constraintSet.clone(constraintLayout)
        constraintSet.connect(editText.id,ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,16)
        constraintSet.connect(editText.id,ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT,16)
        constraintSet.applyTo(constraintLayout)


    }
}

The Kotlin code example above should create and add a new EditText UI Component to the view of your Android app.  I have a few more similar code examples in Kotlin available in Kotlin category. So, if you are just starting with Kotlin and are looking for simple How to’s that page will help you find an example in Kotlin you are looking for.

Also, checkout the books and video courses available on Kotlin below. These resources will help you learn Kotlin and how to build mobile apps for Android with Kotlin much faster!

Kotlin. Video Courses.

The Complete Android Kotlin Developer Course

Learn how to make online games, and apps for Android O, like Pokémon , twitter,Tic Tac Toe, and notepad using Kotlin. The Complete Android Kotlin Developer Course icon

Kotlin for Java Developers

Use your Java skills to learn Kotlin fast. Enhance career prospects and master Kotlin, including Java interoperability. Kotlin for Java Developers icon

Kotlin for Beginners: Learn Programming With Kotlin

Learn Kotlin from scratch! Grasp object-orientation and idiomatic Kotlin to realize coding projects and Android apps! Kotlin for Java Developers icon

Leave a Reply

Your email address will not be published. Required fields are marked *