Possible duplicate of How do I make a Spinner's "disabled" state look disabled?.
I tried the solution mentioned in the post, ie
((Spinner) spinner).getSelectedView().setEnabled(false);
spinner.setEnabled(false);
However getSelectedView() returns null. I also tried manually selecting an item and than calling getSelectedView but it still returns null.
Create a drawable selector in the res/drawable folder like this
custonspinner.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:color="#9D9FA2" />
<item android:color="#000"/>
</selector>
// You can give the drawable also instead of colors in the selector
In your spinner set the background resource like this
android:backgroundResource="@drawable/customspinner"
I figured out the simplest way:
private void setSpinnerEnabled(Spinner spinner, boolean enabled) {
spinner.setEnabled(enabled);
spinner.setAlpha(enabled ? 1.0f : 0.4f);
}
It won't change the color or font, just make it a little more transparent...
One way to do this is to write your own SpinnerAdapter (e.g. an ArrayAdapter) and then override the getView() method. Usually that method inflates the android.R.layout.simple_spinner_item layout and binds the TextView to your data (array in the case of ArrayAdapter). You could take that layout (copy it from your Android SDK folder) and create a disabled version and then do something like that:
private static final int SPINNER_ENABLED = android.R.layout.simple_spinner_item;
private static final int SPINNER_DISABLED = R.layout.simple_spinner_item_disabled;
@Override
public final View getView(int position, View convertView, ViewGroup parent) {
View spinnerView = View.inflate(getContext(), isEnabled(position) ? SPINNER_ENABLED : SPINNER_DISABLED, null);
TextView textView = (TextView) spinnerView.findViewById(R.id.text1);
textView.setText(getItem(position).toString());
return spinnerView;
}
The android.R.layout.simple_spinner_item looks like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
@JSPDeveloper01's answer didn't quite work for me (possibly either due to the order that I'm setting my adapter or due to the fact that I'm using a two custom spinner classes: the first class extends the LinearLayout class; the second extends the Spinner class). The keys are invalidating the old object in the setEnabled function and setting the color in the onDraw function.
Inside both of those custom spinner classes, I have a special setEnabled function like this, invalidating the old view:
public void setEnabled(Boolean enabled) {
super.setEnabled(enabled);
invalidate();
}
I also override the onDraw function in my each custom spinner class (using part of JSPDeveloper01's answer):
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (this.getChildAt(0) != null) {
this.getChildAt(0).setAlpha(this.isEnabled() ? 1.0f : 0.7f);
}
}