AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of recommendations is displayed in a drop-down menu from which the user can choose an item to replace the content of the edit box with.
Add listener
textview.setOnItemClickListener { parent, view, position, id -> //do something }
AutoCompleteTextView in AlertDialog
Display the view in an AlertDialog in a fragment
<?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_marginBottom="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="16dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.AppCompatAutoCompleteTextView android:id="@+id/textview" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
val builder = AlertDialog.Builder(activity!!) builder.setTitle("Add a product") builder.setMessage("") val dialogView = activity!!.layoutInflater.inflate(R.layout.dialog_quotation_product, null) builder.setView(dialogView) builder.setPositiveButton("Finish") { _, _ -> } builder.setNegativeButton("Cancel") { _, _ -> } val dialog: AlertDialog = builder.create() dialog.setCanceledOnTouchOutside(false) dialog.show() val autocompleteView = dialogView.findViewById<AppCompatAutoCompleteTextView>(R.id.textview) val data = listOf("Java", "Swift", "Kotlin", "C++", "C#", "ReactNative", "Unity", "Real Engine") val adapter = ArrayAdapter<String>(activity!!.applicationContext, android.R.layout.simple_list_item_1, data) autocompleteView.setAdapter(adapter) autocompleteView.setOnItemClickListener { parent, view, position, id -> //do something }