How to create Focusable EditText inside ListView on Android using Kotlin?

Kotlin AndroidApps/ApplicationsMobile Development

This example demonstrates how to create Focusable EditText inside ListView on Android using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
   android:descendantFocusability="beforeDescendants" />

Step 3 − Add the following code to src/MainActivity.kt

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.ListView
import java.util.ArrayList
class MainActivity : AppCompatActivity() {
   private lateinit var listView: ListView
   private lateinit var myAdapter: MyAdapter
   override fun onCreate(savedInstanceState: Bundle?) {
      title = "KotlinApp"
      listView = findViewById(
      listView.itemsCanFocus = true
      myAdapter = MyAdapter()
      listView.adapter = myAdapter
   private inner class MyAdapter internal constructor() : BaseAdapter() {
      private var layoutInflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as
      internal var myItems = ArrayList<LauncherActivity.ListItem>()
      lateinit var context: Context
      init {
         for (i in 0..6) {
            val listItem = LauncherActivity.ListItem()
            listItem.className = "Caption$i"
      override fun getItem(position: Int): Any {
         return position
      override fun getItemId(position: Int): Long {
         return position.toLong()
      override fun getCount(): Int {
         return myItems.size
      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
         var convertView: View? = convertView
         val holder: ViewHolder
         if (convertView == null) {
            holder = ViewHolder()
            convertView = layoutInflater.inflate(R.layout.list, null)
            holder.caption = convertView.findViewById(
         } else {
            holder = convertView.tag as ViewHolder
         holder.caption.setText(myItems.get(position).className) = position
         holder.caption.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
               val position =
               val caption = v as EditText
               myItems[position].className = caption.text.toString()
         return convertView
   internal inner class ViewHolder {
      lateinit var caption: EditText

Step 4 − Create a layout resource file (list.xml) and add the following code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
   android:singleLine="true" />

Step 5 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="com.example.q11">
      <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

Click here to download the project code.

Published on 21-Jul-2020 16:03:14