Discussion:
Disable spinner item?
Keith Wiley
2009-12-11 00:36:47 UTC
Permalink
How do I disable (gray out, make unselectable) an item in a Spinner?
I tried calling View.disable() on the associated child of the Spinner,
but it remains black text (not gray) and selectable.

There must be some way of showing but leaving unselectable certain
items in a spinner, right? This is a standard "menu" behavior that
goes back decades. Android permits it, right? I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.

Thanks a lot. I'm sure I'm just missing something obvious here.

Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
rexowner
2009-12-12 00:50:37 UTC
Permalink
I also want to do this, and after hunting around for half a day, have
not found a way to do it,
and am moving on to some other priorities.

I also would be very interested in this useful behavior.

Sorry I can't be of more help, but if you find a way, please post.

Thanks!
Post by Keith Wiley
How do I disable (gray out, make unselectable) an item in a Spinner?
I tried calling View.disable() on the associated child of the Spinner,
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable certain
items in a spinner, right?  This is a standard "menu" behavior that
goes back decades.  Android permits it, right?  I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot.  I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Romain Guy
2009-12-13 22:10:29 UTC
Permalink
This is performed by the adapter:
http://d.android.com/reference/android/widget/ListAdapter.html#isEnabled(int)
Post by rexowner
I also want to do this, and after hunting around for half a day, have
not found a way to do it,
and am moving on to some other priorities.
I also would be very interested in this useful behavior.
Sorry I can't be of more help, but if you find a way, please post.
Thanks!
Post by Keith Wiley
How do I disable (gray out, make unselectable) an item in a Spinner?
I tried calling View.disable() on the associated child of the Spinner,
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable certain
items in a spinner, right?  This is a standard "menu" behavior that
goes back decades.  Android permits it, right?  I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot.  I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
***@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
rexowner
2009-12-15 06:49:26 UTC
Permalink
Thank you for your reply.

None of the 2 Class methods or 8 inherited methods on the page cited
appear to have
anything to do with disabling items in the list. None of the
subclasses
appear to have a way to do this either.

Maybe I am dense, but it is not at all clear how to do this.
This is performed by the adapter:http://d.android.com/reference/android/widget/ListAdapter.html#isEnab...)
Post by rexowner
I also want to do this, and after hunting around for half a day, have
not found a way to do it,
and am moving on to some other priorities.
I also would be very interested in this useful behavior.
Sorry I can't be of more help, but if you find a way, please post.
Thanks!
How do Idisable(gray out, make unselectable) an item in aSpinner?
I tried calling View.disable() on the associated child of theSpinner,
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable certain
items in aspinner, right?  This is a standard "menu" behavior that
goes back decades.  Android permits it, right?  I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot.  I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Romain Guy
2009-12-15 06:54:28 UTC
Permalink
I just linked to it. ListAdapter.isEnabled(int).
Post by rexowner
Thank you for your reply.
None of the 2 Class methods or 8 inherited methods on the page cited
appear to have
anything to do with disabling items in the list.  None of the
subclasses
appear to have a way to do this either.
Maybe I am dense, but it is not at all clear how to do this.
This is performed by the adapter:http://d.android.com/reference/android/widget/ListAdapter.html#isEnab...)
Post by rexowner
I also want to do this, and after hunting around for half a day, have
not found a way to do it,
and am moving on to some other priorities.
I also would be very interested in this useful behavior.
Sorry I can't be of more help, but if you find a way, please post.
Thanks!
How do Idisable(gray out, make unselectable) an item in aSpinner?
I tried calling View.disable() on the associated child of theSpinner,
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable certain
items in aspinner, right?  This is a standard "menu" behavior that
goes back decades.  Android permits it, right?  I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot.  I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
***@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
extrapedestrian
2010-03-13 15:20:26 UTC
Permalink
I did another workaround.

Since I implement SpinnerAdapter, In function GetDropdownView, I check
if item at position x should be disabled, then I make that view (row)
disabled. Unfortunately, SetEnabled(false) is not working on this
view, so I found workaround, like this:

row.SetClickable(true); //makes item unresponsive to clicks(?)
//grayout text and icon
TextView.SetTextColor(Color.LTGRAY);
Image.getDrawable().mutate().setAlpha(70);

so it looks disabled and doesn't respond to clicks.

hate workarounds...
Hi there !
I spent a bit of time trying to create a spinner that would have some
disabled items, and finally found a SOLUTION ! Yay !
Romain pointed in the right direction, BUT it's not that easy.
If one try to create a specific adapter for the spinner, for
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,filters) {
  public boolean isEnabled(int position) {
    // Specific code to decide whether it's enabled or not
  }
  public boolean areAllItemsEnabled() {
    return false;
  }};
spinner.setAdapter(spinnerAdapter);
=> DOESN'T work yet
When you click on a spinner, it opens a dialog with the items feeded
from the adapter.
BUT the adapter is wrapped by the spinner class into a DropDownAdapter
(private class inside the spinner class).
This wrapper delegates almost all method calls, except...
public boolean areAllItemsEnabled() {
  return true;
}
public boolean isEnabled(int position) {
  return true;
}
I don't know if there's a real reason for that. The delegate is
wrapped as as SpinnerAdapter, which doesn't hold those methods.
So the solution was pretty simple thought a bit dirty : extends
Spinner, override performClick() (this is were the adapter is
wrapped), and basically do the same thing as a spinner, but with a
/**
 * A patcher spinner so that the wrapping drop down adapter delegates
 * areAllItemsEnabled and isEnabled calls to the wrapped adapter.
 */
public class PatchedSpinner extends Spinner {
    public PatchedSpinner(Context context, AttributeSet attrs, int
defStyle) {
        super(context, attrs, defStyle);
    }
    public PatchedSpinner(Context context) {
        super(context);
    }
    public PatchedSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public boolean performClick() {
        // boolean handled = super.performClick(); => this line
removed, we do not want to delegate the click to the spinner.
        Context context = getContext();
        final DropDownAdapter adapter = new DropDownAdapter(getAdapter
());
        CharSequence mPrompt = getPrompt();
        AlertDialog.Builder builder = new AlertDialog.Builder
(context);
        if (mPrompt != null) {
            builder.setTitle(mPrompt);
        }
        builder.setSingleChoiceItems(adapter, getSelectedItemPosition
(), this).show();
        return true;
    }
    private static class DropDownAdapter implements ListAdapter,
SpinnerAdapter {
        private SpinnerAdapter mAdapter;
        public DropDownAdapter(SpinnerAdapter adapter) {
            mAdapter = adapter;
        }
        public int getCount() {
            return mAdapter == null ? 0 : mAdapter.getCount();
        }
        public Object getItem(int position) {
            return mAdapter == null ? null : mAdapter.getItem
(position);
        }
        public long getItemId(int position) {
            return mAdapter == null ? -1 : mAdapter.getItemId
(position);
        }
        public View getView(int position, View convertView, ViewGroup
parent) {
            return getDropDownView(position, convertView, parent);
        }
        public View getDropDownView(int position, View convertView,
ViewGroup parent) {
            return mAdapter == null ? null : mAdapter.getDropDownView
(position, convertView, parent);
        }
        public boolean hasStableIds() {
            return mAdapter != null && mAdapter.hasStableIds();
        }
        public void registerDataSetObserver(DataSetObserver observer)
{
            if (mAdapter != null) {
                mAdapter.registerDataSetObserver(observer);
            }
        }
        public void unregisterDataSetObserver(DataSetObserver
observer) {
            if (mAdapter != null) {
                mAdapter.unregisterDataSetObserver(observer);
            }
        }
        // PATCHED
        public boolean areAllItemsEnabled() {
            if (mAdapter instanceof BaseAdapter) {
                return ((BaseAdapter) mAdapter).areAllItemsEnabled();
            } else {
                return true;
            }
        }
        // PATCHED
        public boolean isEnabled(int position) {
            if (mAdapter instanceof BaseAdapter) {
                return ((BaseAdapter) mAdapter).isEnabled(position);
            } else {
                return true;
            }
        }
        public int getItemViewType(int position) {
            return 0;
        }
        public int getViewTypeCount() {
            return 1;
        }
        public boolean isEmpty() {
            return getCount() == 0;
        }
    }
}
If you think I'm doing things wrong, please let me know. This patched
spinner suits my needs.
Cheers,
Piwaï
PS : BTW, merci Romain pour le hint dans la bonne direction.
Post by Romain Guy
I just linked to it. ListAdapter.isEnabled(int).
Post by rexowner
Thank you for your reply.
None of the 2 Class methods or 8 inherited methods on the page cited
appear to have
anything to do with disabling items in the list.  None of the
subclasses
appear to have a way to do this either.
Maybe I am dense, but it is not at all clear how to do this.
This is performed by the adapter:http://d.android.com/reference/android/widget/ListAdapter.html#isEnab...)
Post by rexowner
I also want to do this, and after hunting around for half a day, have
not found a way to do it,
and am moving on to some other priorities.
I also would be very interested in this useful behavior.
Sorry I can't be of more help, but if you find a way, please post.
Thanks!
How do Idisable(gray out, make unselectable) an item in aSpinner?
I tried calling View.disable() on the associated child of theSpinner,
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable certain
items in aspinner, right?  This is a standard "menu" behavior that
goes back decades.  Android permits it, right?  I want the user to see
that certain options are possible under certain circumstances even if
they are impossible at the immediate moment...exactly the way items in
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot.  I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Lisa Neigut
2013-04-29 21:15:34 UTC
Permalink
Following up on extrapedestrian's comment, this is what worked for me.

Inside a custom OptionsSpinnerAdapter (extends ArrrayAdapter implements
SpinnerAdapter):

@Override
public View getDropDownView(int position, View convertView, ViewGroup
parent) {

View view = convertView;

... code to set up view and text ...

if (!option.isAvailable()) {
view.setEnabled(false); // greys out the text
view.setClickable(true); // makes item unclickable
}

return view;
}
Post by extrapedestrian
I did another workaround.
Since I implement SpinnerAdapter, In function GetDropdownView, I check
if item at position x should be disabled, then I make that view (row)
disabled. Unfortunately, SetEnabled(false) is not working on this
row.SetClickable(true); //makes item unresponsive to clicks(?)
//grayout text and icon
TextView.SetTextColor(Color.LTGRAY);
Image.getDrawable().mutate().setAlpha(70);
so it looks disabled and doesn't respond to clicks.
hate workarounds...
Hi there !
I spent a bit of time trying to create a spinner that would have some
disabled items, and finally found a SOLUTION ! Yay !
Romain pointed in the right direction, BUT it's not that easy.
If one try to create a specific adapter for the spinner, for
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,filters) {
public boolean isEnabled(int position) {
// Specific code to decide whether it's enabled or not
}
public boolean areAllItemsEnabled() {
return false;
}};
spinner.setAdapter(spinnerAdapter);
=> DOESN'T work yet
When you click on a spinner, it opens a dialog with the items feeded
from the adapter.
BUT the adapter is wrapped by the spinner class into a DropDownAdapter
(private class inside the spinner class).
This wrapper delegates almost all method calls, except...
public boolean areAllItemsEnabled() {
return true;
}
public boolean isEnabled(int position) {
return true;
}
I don't know if there's a real reason for that. The delegate is
wrapped as as SpinnerAdapter, which doesn't hold those methods.
So the solution was pretty simple thought a bit dirty : extends
Spinner, override performClick() (this is were the adapter is
wrapped), and basically do the same thing as a spinner, but with a
/**
* A patcher spinner so that the wrapping drop down adapter delegates
* areAllItemsEnabled and isEnabled calls to the wrapped adapter.
*/
public class PatchedSpinner extends Spinner {
public PatchedSpinner(Context context, AttributeSet attrs, int
defStyle) {
super(context, attrs, defStyle);
}
public PatchedSpinner(Context context) {
super(context);
}
public PatchedSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean performClick() {
// boolean handled = super.performClick(); => this line
removed, we do not want to delegate the click to the spinner.
Context context = getContext();
final DropDownAdapter adapter = new DropDownAdapter(getAdapter
());
CharSequence mPrompt = getPrompt();
AlertDialog.Builder builder = new AlertDialog.Builder
(context);
if (mPrompt != null) {
builder.setTitle(mPrompt);
}
builder.setSingleChoiceItems(adapter, getSelectedItemPosition
(), this).show();
return true;
}
private static class DropDownAdapter implements ListAdapter,
SpinnerAdapter {
private SpinnerAdapter mAdapter;
public DropDownAdapter(SpinnerAdapter adapter) {
mAdapter = adapter;
}
public int getCount() {
return mAdapter == null ? 0 : mAdapter.getCount();
}
public Object getItem(int position) {
return mAdapter == null ? null : mAdapter.getItem
(position);
}
public long getItemId(int position) {
return mAdapter == null ? -1 : mAdapter.getItemId
(position);
}
public View getView(int position, View convertView, ViewGroup
parent) {
return getDropDownView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return mAdapter == null ? null : mAdapter.getDropDownView
(position, convertView, parent);
}
public boolean hasStableIds() {
return mAdapter != null && mAdapter.hasStableIds();
}
public void registerDataSetObserver(DataSetObserver observer)
{
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}
public void unregisterDataSetObserver(DataSetObserver
observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}
// PATCHED
public boolean areAllItemsEnabled() {
if (mAdapter instanceof BaseAdapter) {
return ((BaseAdapter) mAdapter).areAllItemsEnabled();
} else {
return true;
}
}
// PATCHED
public boolean isEnabled(int position) {
if (mAdapter instanceof BaseAdapter) {
return ((BaseAdapter) mAdapter).isEnabled(position);
} else {
return true;
}
}
public int getItemViewType(int position) {
return 0;
}
public int getViewTypeCount() {
return 1;
}
public boolean isEmpty() {
return getCount() == 0;
}
}
}
If you think I'm doing things wrong, please let me know. This patched
spinner suits my needs.
Cheers,
Piwaï
PS : BTW, merci Romain pour le hint dans la bonne direction.
Post by Romain Guy
I just linked to it. ListAdapter.isEnabled(int).
Post by rexowner
Thank you for your reply.
None of the 2 Class methods or 8 inherited methods on the page cited
appear to have
anything to do with disabling items in the list. None of the
subclasses
appear to have a way to do this either.
Maybe I am dense, but it is not at all clear how to do this.
http://d.android.com/reference/android/widget/ListAdapter.html#isEnab...)
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
I also want to do this, and after hunting around for half a day,
have
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
not found a way to do it,
and am moving on to some other priorities.
I also would be very interested in this useful behavior.
Sorry I can't be of more help, but if you find a way, please post.
Thanks!
How do Idisable(gray out, make unselectable) an item in aSpinner?
I tried calling View.disable() on the associated child of
theSpinner,
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
but it remains black text (not gray) and selectable.
There must be some way of showing but leaving unselectable
certain
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
items in aspinner, right? This is a standard "menu" behavior
that
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
goes back decades. Android permits it, right? I want the user
to see
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
that certain options are possible under certain circumstances
even if
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
they are impossible at the immediate moment...exactly the way
items in
Post by Romain Guy
Post by rexowner
Post by Romain Guy
Post by rexowner
the main Android menu-button invoked menu can be visible but
unselectable.
Thanks a lot. I'm sure I'm just missing something obvious here.
Cheers!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have
time
Post by Romain Guy
Post by rexowner
Post by Romain Guy
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Romain Guy
Android framework engineer
Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them
--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...