Search code examples
androidautocompletetextview

AutoCompleteTextView items are not clickable when using custom item layout


I'm developing an application that uses AutoCompleteTextView and a custom item layout and I can't make item click action work.

Here is the custom item layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="viewModel" type="blog.liderc.familybudget.vms.DepartmentVM"/>
       <variable name="parentViewModel" type="blog.liderc.familybudget.AddSubjectViewModel"/>
   </data>
   <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:gravity="start"
               android:text="@{viewModel.title}"
               android:textSize="20sp"/>
       <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:gravity="end"
               android:text="x"
               android:textSize="20sp"
               android:onClick="@{() -> parentViewModel.deleteDepartmentAsync(viewModel)}"/>
   </LinearLayout>
</layout>

Here is how I configure that:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
   return activity?.let {
       val builder = AlertDialog.Builder(it)
       val view = LayoutInflater
           .from(it)
           .inflate(R.layout.dialog_add_subject, null)

       viewModel = ViewModelProviders.of(it).get(AddSubjectViewModel::class.java)
       viewModel.subject = SubjectVM()
       viewModel.departmentsAutoComplete = departments
       val app = it.application as FamilyBudgetApp
       viewModel.depsRepo = app.depsRepository.value

       val adapter = ObserverListAdapter(
           departments,
           R.layout.department_autocomplete,
           viewModel) { view.department.refreshAutoCompleteResults() }
       view.department.setAdapter(adapter)

       val binding: ViewDataBinding? = DataBindingUtil.bind(view)
       binding?.run {
           setVariable(BR.viewModel, viewModel)
           executePendingBindings()
       }
       builder.setView(view)
           .setPositiveButton("Remember", positive)
           .setNegativeButton("Nevermind") { dialog, _ -> dialog.cancel() }
       builder.create()
   } ?: throw IllegalStateException("Activity cannot be null")
}

R.layout.department_autocomplete - is the custom item layout.

When I type into the corresponding AutoCompleteTextView field suggestions are being filtered and presented correctly but nothing happens when I hit any of them.

Here is how it looks: Autocomplete menu


Solution

  • I am having the same issue as you. You will have to make your own onItemClick event. Following this tutorial, you would need to set your own onItemClickListener.

    It should look like this:

        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // click event (such as filling the autocomplete section)
                }
            });
    

    edit: I did not see your comment. Another thing I had to do was to set android:descendantFocusability="blocksDescendants" in the <LinearLayout> from the xml file. Source