AutoCompleteTextView.isPopupShowing () is always FALSE

I am trying to find out if an AutoCompleteTextView is displayed / drop down . When I click a button, I want to show a drop-down menu (if it's hidden) and hide it (if it's shown). I use the method for this isPopupShowing(), but it always returns FALSE .

Example:

@Override
public void onClick(View view) {

    if (view.getId() == button.getId()) {

        if (autoCompleteTextView.isPopupShowing()) {
            autoCompleteTextView.dismissDropDown();
        } else {
            autoCompleteTextView.showDropDown();
        }   
    }   
}
+4
source share
2 answers

When AutoCompleteTextView has lost focus, the popup menu disappears. Thus, the drop-down menu is always invisible when you click the button.

Just add a new Boolean property to your listener to remember the last state.

+3
source

Code in Kotlin

val afill = findViewById<AutoCompleteTextView>(R.id.myTextId)
var showAFill = false

afill.addTextChangedListener (object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
         showAFill = afill.isPopupShowing
    }
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }
    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int)
    }
})

afill.onItemClickListener = object : AdapterView.OnItemClickListener {
    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
        showAFill = false
    }
}

, .

showAutofill.setOnClickListener { _ ->
    if (showAFill) afill.dismissDropDown()
    else afill.showDropDown()
    showAFill = !showAFill
}

showAutofill -

0

All Articles