When I disable my Spinner it looks almost exactly like it did prior to being disabled, i.e.
Before
For future reference, if you're using Kotlin, you can make use of extension functions, and provide a custom behaviour for disabled elements:
fun Spinner.forceEnabled(isEnabled : Boolean){
setEnabled(isEnabled)
getChildAt(0)?.let{ childView ->
childView.alpha = if (this.isEnabled) 1.0f else 0.33f
}
invalidate()
}
someSpinner.forceEnabled(true)
This will allow to set custom properties to spinner children views, as the spinner is being disabled, without need for subclassing. Be cautious, as extension functions are resolved statically!
One clever way of making spinners look disabled is to lower the transparency.
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setEnabled(false);
spinner.setAlpha(0.5f);
I found this to be the best solution to the same question that was previously answered by @JSPDeveloper01: https://stackoverflow.com/a/20401876/8041634
Since Android doesn't gray out the spinner when it has been set to disabled, he suggests creating a custom method that uses the .setAlpha
command on the spinner, which grays out the text within it. Brilliant.
((Spinner) spnr).getSelectedView().setEnabled(false);
((Spinner) spnr).setEnabled(false);
spnr is my Spinner
object which refers to the XML view file, by findViewById(...)
.
I've tried the following, and it's working as expected for me:
_userMembership.setEnabled(false);
_userMembership.setClickable(false);
_userMembership.setAlpha((float)0.7);
_userMembership.setBackgroundColor(Color.GRAY);
Don't know if you still need this but there is a way. I've been struggling with this issue myself. I ended up doing something like this:
((Spinner) spinner).getSelectedView().setEnabled(false);
spinner.setEnabled(false);
What this actually does is disable the spinner and the selected item that is shown. Most likely the selected item is a TextView and it should show as a disabled TextView.
I am using this and it works. But for some reason unknown to me it is not as "greyed-out" as other disabled views. It still looks disabled though. Try it out.