how to remove text that appears on a spinner control in android?
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?
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.
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" />
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" />
精彩评论