androidkotlinautocompletetextviewonitemclicklisteneronitemlongclicklistener

How to Set Multiple Click Listener to AutoCompleteTextView in Android?


I have an AutoCompleteTextView that will show a list of Hints when type in few keyword that match with its ArrayAdapter Data.

Currently, I have set an AdapterView.OnItemClickListener so when user click on the Hint, the soft keyboard on screen will be close.

What I trying to achieve :

I been search for how to set LongClickListener on Autocomplete, however I could not find any solution on the net.

I believe it is important to ensure the Click Listen able to obtain the position(in Int) or text data(in String) of the Hint as I would need to determine what are the Data that being selected and I can tell the Database to delete it.

My Questions:

How can I set a Long Click Listener for the AutoCompleteTextView that will also obtain the selected Hint position?

Any other solution that could help to resolve what I trying to achieve is welcome, thank you.

Note: I would like to keep the functionality of the hide soft keyboard when Hint is selected. I accept Answer In Java Language as well.

What I have done so far :

 // Get an ArrayList<String> from database and declare to remarkList
 val remarkList: ArrayList<String> = getDataFromDatabase() 

 // Set remarkList Data into ArrayAdapter
 val adapter = ArrayAdapter(context!!, android.R.layout.simple_list_item_1, remarkList)

 // Set ArrayAdapter to AutoCompleteTextView
 autoComplete_remarks.setAdapter<ArrayAdapter<String>>(adapter)

        // When click the hint selection, will trigger close keyboard function
        autoComplete_remarks.onItemClickListener =
                AdapterView.OnItemClickListener { _: AdapterView<*>, _: View, position: Int, _: Long ->

                    hideKeyboard(activity!!)
                }

Solution

  • Have created the Custom Adapter Class for the AutoCompleteTextView, Basically This Custom Adapter, in the getView function will create an Unique Individual Listener for each TextView that will be pop out by the AutoCompleteTextView.

    Note: The TextView results that pop out by the AutoCompleteTextView is Customizable through inner class ListFilter: Filter() and override fun publishResults(constraint: CharSequence?, results: FilterResults) function

    Custom Adapter Class

    class AutoCompleteTextViewCustomAdapter(context: Context, resource: Int, data: ArrayList<String>): ArrayAdapter<String>(context, resource) {
    
        private var mListener: IOnItemListener? = null
    
        private var dataList: List <String>? = data
        private val listFilter = ListFilter()
        private var dataListAllItems: List<String>? = null
    
        override fun getView(position: Int, view: View? , parent: ViewGroup): View {
            var adapterView = view
    
            // using Custom XML View
            if (adapterView == null) {
                    adapterView = LayoutInflater.from(parent.context)
                        .inflate(R.layout.list_row_text, parent, false)
            }
    
            val textView = adapterView!!.findViewById(R.id.textView) as TextView
            textView.text = getItem(position)
    
            // Custom OnClickListener Setup
            textView.setOnLongClickListener {
                mListener?.onLongClick("Pass Data")
                true
            }
            // Custom OnClickListener Setup
            textView.setOnClickListener {
                mListener?.onSingleClick("Pass Data")
            }
    
            return adapterView
        }
    
        // Custom OnClickListener Setup
        fun setListener(listener : IOnItemListener) {
            mListener = listener
        }
    
        // Custom OnClickListener Setup that will be Called from the Activity/Fragment
        interface IOnItemListener {
            fun onLongClick(dataToBePass : String)
            fun onSingleClick(dataToBePass : String)
        }
    
        // Custom Adapter Setup for AutoCompleteTextView
        override fun getCount(): Int {
            return dataList!!.size
        }
    
        override fun getItem(position: Int): String ? {
            return dataList!![position]
        }
    
        override fun getFilter() : Filter {
            return listFilter
        }
    
        inner class ListFilter: Filter() {
            ..// Filtering Logic
            return results
        }
    
        override fun publishResults(constraint: CharSequence?, results: FilterResults) {
            ..// Filtering Logic
        }
    }
    

    So, when each TextView that pop out by the AutoCompleteTextView being onClick or onLongClick, the override method in AutoCompleteTextViewCustomAdapter.IOnItemListener will be called to perform logic function that you desire.

    Activity/Fragment

    val adapter = AutoCompleteTextViewCustomAdapter(context!!, R.layout.list_row_text, dataList).also {
    
                //Setup the OnClickListener what to perform when the TextView of the Adapter if being onClick
                it.setListener(object : AutoCompleteTextViewCustomAdapter.IOnItemListener {
                    override fun onLongClick(dataReceived: String) {
                        // DO SOMETHING
                    }
    
                    override fun onSingleClick(dataReceived: String) {
                        // DO SOMETHING
                    }
                })
            }
    
            // Set the adapter to the AutoCompleteTextView View that define in your XML File
            autoCompleteTextView.setAdapter(adapter)
    

    So far this solution work for my Scenario, in short, to having a Multiple Click Listener to AutoCompleteTextView, we cannot use the Default Adapter as they only support Single onClickListener, so we have to Create Custom Adapter to customize and setup ALL the ClickListener to support multiple onClick Listener.