If you want, there is a decorater spinnerAdapter witch add automatically a default value :
protected class SpinnerAdapterWithNoValue implements SpinnerAdapter {
private SpinnerAdapter _current;
private final static String defaultValue = "Choisir";
public SpinnerAdapterWithNoValue(SpinnerAdapter base) {
_current = base;
}
@Override
public int getCount() {
return _current.getCount() + 1;
}
@Override
public Object getItem(int position) {
if (position == 0 || position == -1) {
return null;
}
return _current.getItem(position - 1);
}
@Override
public long getItemId(int position) {
if (position == 0 || position == -1) {
return -1;
}
return _current.getItemId(position - 1);
}
@Override
public int getItemViewType(int position) {
if (position == 0 || position == -1) {
return -1;
}
return _current.getItemViewType(position - 1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0 || position == -1) {
final TextView v = (TextView) ((LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_text, parent, false);
v.setText(defaultValue);
return v;
}
return _current.getView(position - 1, convertView, parent);
}
@Override
public int getViewTypeCount() {
return _current.getViewTypeCount();
}
@Override
public boolean hasStableIds() {
return _current.hasStableIds();
}
@Override
public boolean isEmpty() {
return _current.isEmpty();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
_current.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
_current.unregisterDataSetObserver(observer);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (position == 0 || position == -1) {
CheckedTextView v = (CheckedTextView) ((LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.simple_spinner_dropdown_item, parent,
false);
v.setText(defaultValue);
return v;
}
return _current.getDropDownView(position - 1, convertView, parent);
}
}
Then you can create your own spinner using this decorater :
public class SpinnerWithNoValue extends Spinner {
public SpinnerWithNoValue(Context context) {
super(context);
}
public SpinnerWithNoValue(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpinnerWithNoValue(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setAdapter(SpinnerAdapter orig) {
final SpinnerAdapter adapter = new SpinnerAdapterWithNoValue(orig);
super.setAdapter(adapter);
try {
final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
m.setAccessible(true);
m.invoke(this, -1);
final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
n.setAccessible(true);
n.invoke(this, -1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/*
* getSelectedItem renvoi null si la valeur par defaut est séléctionnée
*
* @see android.widget.AdapterView#getSelectedItem()
*/
@Override
public Object getSelectedItem() {
return super.getSelectedItem();
}
}
You just have to change the spinner declaration in your xml layout :
com.myproject.SpinnerWithNoValue
If you want, you can change the code to set the default text in the tag of your spinner.