0

By default there is a text that comes up on a spinner control... .this text is the first value in the array that i have associated with the adapter for the spinner.

Upon selection this text changes to the value that i have selected.

I want to remove this text that appears on the spinner control. How do I go about it?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
user590849
  • 11,655
  • 27
  • 84
  • 125
  • You don't want to show the text if a value is selected or you don't want to have a preselected value? – Flo May 13 '11 at 07:53
  • Possible duplicate of [How to make an Android Spinner with initial text "Select One"](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – blahdiblah Mar 09 '13 at 11:44

3 Answers3

2

To hide the selected text when the spinner is "closed", you have to define a custom adapter and a layout. To get an idea what this is all about I suggest that you read this blog entry.

So the steps you have to take would be:

  • Define a custom layout for the spinner when it is "closed". You can also define a custom layout for the dropdown mode of the spinner if you want to.
  • Implement a custom adapter. You have to override the getView() method and adapt it to your custom layout. In your case you would simply create and inflate the custom layout as you don't want to show any values you don't need to bind them to the view. If you also want a custom drowdown layout you also have to override the getDropDownView() method.
  • In your activity: Create a instance of you custom adapter, bind your data source (array or cursor) to it and bind the adapter to your spinner.
Community
  • 1
  • 1
Flo
  • 27,355
  • 15
  • 87
  • 125
1

add new class

public class MySpinner extends Spinner {

    private Context _context;

    public MySpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        _context = context;
    }

    public MySpinner(Context context) {
        super(context);
        _context = context;
    }

    public MySpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context);
        _context = context;
    }

    @Override
    public View getChildAt(int index) {
        View v = new View(_context);
        return v;
    }

}

in your layout

<[package].MySpinner android:id="@+id/Spinner01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
George
  • 1,327
  • 11
  • 25
  • This makes the spinner text blink for a few milliseconds and then dissappear. – Felipe Lima Apr 19 '13 at 22:27
  • Perfect I am using a spinner in Action Bar, the text goes but the icon resizes according to the textview which is the longest in the drop down. – Skynet Jan 15 '14 at 07:22
0

Example of @Flo Answer.

You can override GetView() method of Arrayadapter class and set view accordingly for selected item.

public class CustomArrayAdapter : ArrayAdapter
{
    private Context context;
    public CustomArrayAdapter(Context context, int resource, int textViewResourceId, IList objects) : base(context, resource, textViewResourceId, objects)  
    {
        this.context = context;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View FilterIconView =  LayoutInflater.From(context).Inflate(Resource.Layout.FilterIconLayout, parent, false);
        FilterIconView.FindViewById<ImageView>(Resource.Id.filterIcon).SetImageResource(Resource.Drawable.filterSolid);
        return FilterIconView;
    }
}

This is My FilerIconLayout.xml file

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/filterIcon"
    android:contentDescription="description"
    tools:ignore="XmlNamespace"
    android:scaleType="fitXY" />
Avii
  • 25
  • 5