After setting the adapter. call setSelection (i used with 0) and right after that set the text color to transparent.
// Preselect the first to make the spinner text transparent
spinner.setSelection(0, false);
TextView selectedView = (TextView) spinner.getSelectedView();
if (selectedView != null) {
selectedView.setTextColor(getResources().getColor(R.color.transparent));
}
Then, set your OnItemSelectedListener (if needed).
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This will make the spinner empty at first time seen.
But, if the user will select the first item it will do nothing because 0 is pre selected. For fixing this i used this subclass of spinner. taken from @melquiades's answer:
/**
* Spinner extension that calls onItemSelected even when the selection is the same as its previous value
*/
public class FVRSpinner extends Spinner {
public FVRSpinner(Context context) {
super(context);
}
public FVRSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FVRSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
if (getOnItemSelectedListener() != null) {
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
@Override
public void setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
if (getOnItemSelectedListener() != null) {
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
}