androidspinnerlong-press

On Android, is there any way to disable a Spinner's long-press behavior?


The default spinner behavior is that when it's "closed", longpressing on it will "open" it and show you the dropdown view. I find this behavior to be potentially very problematic for the user. For example, if they're trying to scroll something on the screen, and happen to "grab" a spot that has a spinner, then instead of scrolling, it'll open the dropdown view after a second or so, and the user is basically left with their finger on one of the dropdown options (which they can now accidentally press).

So, I'd like to disable that long-press behavior, and have the spinner "open" when only it's clicked, and not long-pressed. Is that possible?


Solution

  • So, I've figured out a relatively easy way to do this, even though it's not very elegant. Basically, I created a transparent overlay view on top of the Spinner, and set it to have an OnClickListener that just triggers a Spinner's click.

    XML:

        <Spinner 
            android:id="@+id/spinner"
            android:layout_width="match_parent" 
            android:layout_height="40dip" />
    
        <View android:id="@+id/spinner_overlay"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/spinner" 
            android:layout_alignRight="@id/spinner"
            android:layout_alignTop="@id/spinner" 
            android:layout_alignBottom="@id/spinner"  />
    

    Java:

            View spinnerOverlay = findViewById(R.id.spinner_overlay);
            spinnerOverlay.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    spinner.performClick();
                }
    
            });