77

I'm having problems when using android:enabled="false", it's not disabling the component in the case it's a spinner. Don't know if it's relevant, but it belongs to a layout that's part of a viewflipper.

Any hints or workarounds ?

Thanks

Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
xain
  • 13,159
  • 17
  • 75
  • 119

5 Answers5

119

Disable or enable it before setting the adapter.

yourSpinner.setEnabled(false);   
yourSpinner.setClickable(false);  
yourSpinner.setAdapter(typeAdapter);
CaptJak
  • 3,592
  • 1
  • 29
  • 50
himb2001
  • 1,371
  • 1
  • 10
  • 7
  • 3
    I haven't found that including `setClickable` is necessary, at least not on API 24. Has anyone found that it is on some other version of android? – Jon Dec 22 '16 at 21:24
  • 1
    More importantly, as of today, spinners don't respect the clickable attribute at all, whether it was set in XML or in code. – Zonker.in.Geneva May 06 '20 at 11:46
  • 2
    it has nothing to do with setting the adapter, you can do it before or after, it doesn't matter, why did you even mention it? – user924 May 20 '20 at 10:04
  • @user924 yes, it matters, after setting the adapter it does not work. – Christian Mar 22 '23 at 21:01
31

It's not possible to enable/disable a Spinner in XML (yet). To do so you have to do it in code.

Here's an example:

Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setEnabled(false);
The Berga
  • 3,744
  • 2
  • 31
  • 34
20

you can set android:clickable="false" in the xml to disable the spinner for click event.

Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
Zephyr
  • 6,123
  • 34
  • 33
12

You can set this in the Java code itself, instead of in the XML, because the Spinner should implement setEnabled(boolean) from View.

Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
AlbeyAmakiir
  • 2,217
  • 6
  • 25
  • 48
-1
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setEnabled(false);

Will not work

Actual code that will work ...

Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setEnabled = false;
RusArtM
  • 1,116
  • 3
  • 15
  • 22