Writing on Kotlin ListView With EditText Android example tutorial.
You will learn how to Get or Set text Values Of EditText In ListView using Kotlin in Android.
This tutorial will solve the android edittext in listview focus problem.
We will be able to add any text in the any row’s edittext of the listview. Then we will send the the text values of all the EditTexts to the next activity.
First of all, see the following video for output reference.
Step 1. Separate XML
Create a new project in the android studio with empty activity as the default one and kotlin as the primary language.
Now in the app->res->layout directory, make a new XML file and give it a name like lv_item.xml
You should add the below lines in lv_item.xml file.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:textColor="#000" android:text="TextView:"/> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/editid" android:layout_marginTop="10dp" android:paddingLeft="10dp" android:textColor="#000" android:text="hello" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="@color/colorAccent"/> </LinearLayout>
One text view and one edit text is there in the above file.
We will not change the value of the text view but user can edit the text of the edit text.
Step 2. Model and Adapter Files
Create a new Kotlin class and give it a name like EditModel.kt
Write down the following code lines into the EditModel.kt file.
class EditModel { var editTextValue: String? = null fun getEditTextValues(): String { return editTextValue.toString() } fun setEditTextValues(editTextValue: String) { this.editTextValue = editTextValue } }
There is only one variable is there in this file : editTextvalue
I have also written getter and setter methods for this string variable.
These methods will help us to maintain data among the various child items of the edit text.
Time to create an adapter for the listview. Make a new class with the name EditAdapter.kt
EditAdapter.kt file should contain the following source code lines
import android.content.Context import android.text.Editable import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.EditText import java.util.ArrayList /** * Created by Parsania Hardik on 03-Jan-17. */ class EditAdapter(private val context: Context, editModelArrayList2: ArrayList<EditModel>) : BaseAdapter() { init { editModelArrayList = editModelArrayList2 } override fun getViewTypeCount(): Int { return count } override fun getItemViewType(position: Int): Int { return position } override fun getCount(): Int { return editModelArrayList.size } override fun getItem(position: Int): Any { return editModelArrayList[position] } override fun getItemId(position: Int): Long { return 0 } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { var convertView = convertView val holder: ViewHolder if (convertView == null) { holder = ViewHolder() val inflater = context .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater convertView = inflater.inflate(R.layout.lv_item, null, true) holder.editText = convertView!!.findViewById(R.id.editid) as EditText convertView.tag = holder } else { // the getTag returns the viewHolder object set as a tag to the view holder = convertView.tag as ViewHolder } holder.editText!!.setText(editModelArrayList[position].getEditTextValues()) holder.editText!!.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { editModelArrayList[position].setEditTextValues(holder.editText!!.text.toString()) } override fun afterTextChanged(editable: Editable) { } }) return convertView } private inner class ViewHolder { var editText: EditText? = null } companion object { lateinit var editModelArrayList: ArrayList<EditModel> } }
First of all, see the following code
if (convertView == null) { holder = ViewHolder() val inflater = context .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater convertView = inflater.inflate(R.layout.lv_item, null, true) holder.editText = convertView!!.findViewById(R.id.editid) as EditText convertView.tag = holder } else { // the getTag returns the viewHolder object set as a tag to the view holder = convertView.tag as ViewHolder } holder.editText!!.setText(editModelArrayList[position].getEditTextValues())
This code is from getView() method. Here, compiler will inflate the layout file lv_item.xml
Thus, every child row of the list view will contain the view that is created by the lv_item.xml file.
Then compiler will find the edit text using the id. After this, it will set the text into the edit text. Here, compiler is using editModelArrayList as the data source.
Adapter is getting this editModelArrayList from the main activity via it’s parameter. We will develop this data source (editModelArrayList ) in the Main activity.
Now focus on the following
holder.editText!!.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { editModelArrayList[position].setEditTextValues(holder.editText!!.text.toString()) } override fun afterTextChanged(editable: Editable) { } })
Above source code is also from getView() method. When the user changes the text of the edit text, compiler will execute the onTextChanged() method.
Step 3. Writing Main File
In to your activity_main.xml file, you need to copy paste the below code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="Pass All EditText value to Next Activity"/> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:divider="@null"/> </LinearLayout>
In the layout of the main activity, there is one button and list view. A button click will open the new activity.
Now below is the source snippet for the MainActivity.kt file.
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.ListView import android.content.Intent import java.util.ArrayList class MainActivity : AppCompatActivity() { private var btn: Button? = null private var lv: ListView? = null private var customeAdapter: EditAdapter? = null lateinit var editModelArrayList: ArrayList<EditModel> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) lv = findViewById(R.id.listView) as ListView btn = findViewById(R.id.btn) as Button editModelArrayList = populateList() customeAdapter = EditAdapter(this, editModelArrayList) lv!!.adapter = customeAdapter btn!!.setOnClickListener { val intent = Intent(this@MainActivity, NextActivity::class.java) startActivity(intent) } } private fun populateList(): ArrayList<EditModel> { val list = ArrayList<EditModel>() for (i in 0..7) { val editModel = EditModel() editModel.setEditTextValues(i.toString()) list.add(editModel) } return list } }
First of all, check the below
private var btn: Button? = null private var lv: ListView? = null private var customeAdapter: EditAdapter? = null lateinit var editModelArrayList: ArrayList<EditModel>
Compiler will create the objects of the button, list view and EditAdapter class.
Then it will create one arraylist (editModelArrayList) with the objects of the EditModel class.
Now see the following snippet
editModelArrayList = populateList() customeAdapter = EditAdapter(this, editModelArrayList) lv!!.adapter = customeAdapter
Compiler will populate the editModelArrayList using the populateList() method.
Then it will initialize the object of the adapter and then after, it will set this adapter to the list view.
Below is the coding lines for the populateList() method.
private fun populateList(): ArrayList<EditModel> { val list = ArrayList<EditModel>() for (i in 0..7) { val editModel = EditModel() editModel.setEditTextValues(i.toString()) list.add(editModel) } return list }
This method is having one for loop.
Inside this for loop, it will create the object of the EditModel class. Then it will set the edit text value for this object and then it will add this object to the arraylist.
Compiler will read the below source code when the user clicks the button.
btn!!.setOnClickListener { val intent = Intent(this@MainActivity, NextActivity::class.java) startActivity(intent) }
It will simply open the Next activity. Now let us make the next activity.
Step 4. Making Next Activity
Make a new activity and give it a name like NextActivity.
You should have the below code in activity_next.xml file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:id="@+id/tv"/> </RelativeLayout>
There is only one text view is there in this file.
Now in your NextActivity.kt file, you need to write the following source lines
import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.TextView class NextActivity : AppCompatActivity() { private var tv: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_next) tv = findViewById(R.id.tv) as TextView for (i in 0 until EditAdapter.editModelArrayList.size) { tv!!.text = tv!!.text.toString() + " " + EditAdapter.editModelArrayList.get(i).getEditTextValues() + System.getProperty( "line.separator" ) } } }
Here, we will add the value to the text view.
Compiler will create one for loop. Inside it, it will fetch the edit text value using the editModelArrayList which we have defined in the EditAdapter class.
In the every iteration of for loop, compile will fetch the value of one row’s edit text of the list view. Thus, at the end of the loop, our text view will contain the whole list view’s edit text values.
Download Code For Kotlin Listview with Edittext
https://github.com/demonuts/Kotlin-ListView-With-EditText-Get-Set-Values-Of-EditText-In-ListView