2

I use this code to to hide the navigation bar:

View root = findViewById(android.R.id.content);
root.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);     
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

But when a Spinner is touched, the status bar is shown on top of the screen.

This solution seems to work when android:spinnerMode="dropdown"

Field listPopupField = Spinner.class.getDeclaredField("mPopup");
listPopupField.setAccessible(true);
Object listPopup = listPopupField.get(spinner);
if (listPopup instanceof ListPopupWindow) {
    Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
    popupField.setAccessible(true);
    Object popup = popupField.get((ListPopupWindow) listPopup);
    if (popup instanceof PopupWindow) {
        ((PopupWindow) popup).setFocusable(false);
    }
}

Is there a solution for android:spinnerMode="dialog" ?

A.G.
  • 2,037
  • 4
  • 29
  • 40

1 Answers1

0

You need to clear the focus every time you click the spinner as the popupWindow is recreated; but unfortunately there is no direct API for listening to this event.

To overcome this you can create a custom Spinner & override its performClick() which gets triggered when the spinner is clicked to show up the menu. Then you can clear the focus as you shared.

public class DialogSpinner extends Spinner {
    public DialogSpinner(Context context) {
        super(context);
    }

    public DialogSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean performClick() {

        try {
            Field listPopupField = Spinner.class.getDeclaredField("mPopup");
            listPopupField.setAccessible(true);
            Object listPopup = listPopupField.get(this);
            if (listPopup instanceof ListPopupWindow) {
                Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
                popupField.setAccessible(true);
                Object popup = popupField.get(listPopup);
                if (popup instanceof PopupWindow) {
                    ((PopupWindow) popup).setFocusable(false);
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return super.performClick();

    }


}
Zain
  • 37,492
  • 7
  • 60
  • 84