Kotlin Android AutoCompleteTextView I'm not able to select item and get it for search function

53 views Asked by At

I'm trying to use AutoCompleteTextView to implement a search through a list of item in a database. I able to create a drop-down menu when I insert a first char, but I'm not able to select one from the menu and get it to performe my search fuction. According with developer reference I create an ArrayAdapter to address the recources. Following an extract of my xml layout (R.layout.activity_test) and the structure to implement AutoCompleteTextView.

Layout activity_test

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <AutoCompleteTextView
        android:id="@+id/tvAutoComplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Search"
        android:inputType="textAutoComplete"
        android:completionThreshold="1"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Implementation of AutoCompleteTextView

val itemList = ArrayList<String>()

itemList is successfully collected

val autoComplete: AutoCompleteTextView = findViewById(R.id.tvAutoComplete)
val adapter = ArrayAdapter(this,R.layout.activity_test,R.id.tvAutoComplete,itemList)
autoComplete.setAdapter(adapter)

autoComplete.setOnItemClickListener { parent, view, position, id ->
    Log.d("Test ", itemList[position])
}

setOnItemClickListener not working.

Result of my implementation of AutoCompleteTextView

2

There are 2 answers

2
Prem Thakur On

To handle the click events of the item of its drop-down menu use these callbacks:

            //you can use lambda instead of making an object
            autoComplete.onItemClickListener = object : AdapterView.OnItemClickListener{
                override fun onItemClick(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    //Handle the click event
                }
            }

            autoComplete.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    //Handle the selection
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                    TODO("invoked when the selection disappears from this view. " +
                            "The selection can disappear for instance when touch is " +
                            "activated or when the adapter becomes empty.")
                }

            }

The one that you have mentioned in your answer is for handling click events of the AutoCompleteTextView which I think won't get invoked because it is a child of EditText. In EditText, the click listeners don't get invoked.

0
Santi On

Solution that work for me. I create a new xml layout

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvCustom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp"
        android:textStyle="bold"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/design_default_color_primary_dark"/>
    

</LinearLayout>

then I declare this resources into ArrayAdapter:

val adapter: ArrayAdapter<String> = ArrayAdapter(this.applicationContext,R.layout.layout_autocomplete_item,R.id.tvCustom,itemList)

and at last I can provide to clickable menu with:

autoComplete.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
        autoComplete.clearListSelection()
})