I\'ve implemented a spinner into my app. I\'m showing a list of schools but I want a default value/prompt so the user knows to select a school. I also want to do this withou
You should create custom ArrayAdapter with a String for your desired text in its constructor. Then, you should switch the first element of your array with your default text, and after the user click on the spinner it should switch back to the original array.
Example:
Activity class:
public class SpinnerActivity extends Activity {
Spinner spinner;
String defaultTextForSpinner = "Your deafult text here";
String[] arrayForSpinner = {"One", "Two", "Three"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.your_spinner);
spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
}
}
CustomSpinnerAdapter class:
public class CustomSpinnerAdapter extends ArrayAdapter<String>{
Context context;
String[] objects;
String firstElement;
boolean isFirstTime;
public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
super(context, textViewResourceId, objects);
this.context = context;
this.objects = objects;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(isFirstTime) {
objects[0] = firstElement;
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = objects[0];
objects[0] = defaultText;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_text);
label.setText(objects[position]);
return row;
}
}
Try setSelection method to set default text:
spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))
And look this link: How to make an Android Spinner with initial text "Select One". It is very useful (@aaronvargas answer is what you need).
@Override
protected void onPostExecute(PositionBean[] result) {
String[] spinnerArray = new String[result.length];
if(result!=null) {
for (int i = 0; i < result.length; i++) {
String positionName = result[i].getPositionName();
spinnerArray[i] = result[i].getPositionName();
}
ArrayList<String> positionList = new ArrayList<String>(Arrays.asList(spinnerArray));
positionList.add(0,"select");
String [] positions = positionList.toArray(new String[positionList.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineActivity.this, android.R.layout.simple_spinner_item,
positions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
positionSpinner.setAdapter(adapter);
}else{
Log.d("NULL", "This is null");
}
}