Android ListPopupWindow's method isShowing() does not work
Asked Answered
G

3

5

I've decided to create my own custom spinner by extending a TextView and composing a ListPopupWindow. I want to emulate the following functionality of the original Spinner: when the spinner is clicked the drop down list is shown, the second time the spinner is clicked the drop down list is dismissed. But I'm having some trouble, the ListPopupWindow.isShowing() seems to always return false (I've debugged it):

public class CustomSpinner extends TextView {
    ...
    private ListPopupWindow dropDownPopup;
    ...
    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        ...
        dropDownPopup = new ListPopupWindow(context, attrs);
        dropDownPopup.setAnchorView(this);
        dropDownPopup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        dropDownPopup.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dropDownPopup.dismiss();
                ...
            }
        });     

        this.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                
                if (dropDownPopup.isShowing()) {
                    dropDownPopup.dismiss();
                } else {
                    dropDownPopup.show();
                }
            }
        });
    }

So, each time I click on the spinner the drop down list is shown. It is dismissed when I click on one of the items in the list. The problem seems to be that dropDownPopup.isShowing() always returns false.

Gelatinate answered 22/4, 2014 at 8:55 Comment(0)
G
12

By setting dropDownPopup.setModal(true), everything works.

Gelatinate answered 22/4, 2014 at 12:49 Comment(1)
That's weird, with setModal(true) my onClick listener is not called at all when the popup is opened, but the popup disappearsMcquoid
S
0

By adding dropDownPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED);beforedropDownPopup.show();, it works for me.

Sawyers answered 8/1, 2020 at 3:43 Comment(0)
Q
0

When clicking outside the popup it is immediately dismissed, before the OnClickListener for the CustomSpinner is called, so in the OnClickListener isShowing is always false.

As a workaround you can attach an OnDismissListener to the popup which will set an isDismissing variable to true and post a message which will set isDismissing to false with a short delay, and check isDismissing in the OnClickListener.

Something like this:

dropDownPopup.setOnDismissListener(() -> {
    isDismissing = true;
    this.getHandler().postDelayed(() -> { isDismissing = false; }, 200);
});

this.setOnClickListener((v) -> {
    if (!isDismissing)
        dropDownPopup.show();
});

where isDismissing is a member of the class initially set to false.

Quid answered 4/7 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.