Created
May 31, 2016 09:29
-
-
Save MostafaAnter/3186d947cddeb5075acf8caa18b2bcc1 to your computer and use it in GitHub Desktop.
you can add hint and change font and more control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void populateSpinner1(){ | |
// you will just change R.array.search & spinner1 reference :) | |
final List<String> plantsList = Arrays.asList(getResources().getStringArray(R.array.search1)); | |
// Initializing an ArrayAdapter | |
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>( | |
this,R.layout.spinner_item, plantsList){ | |
@Override | |
public boolean isEnabled(int position){ | |
if(position == 0) | |
{ | |
// Disable the first item from Spinner | |
// First item will be use for hint | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, | |
ViewGroup parent) { | |
View view = super.getDropDownView(position, convertView, parent); | |
TextView tv = (TextView) view; | |
if(position == 0){ | |
// Set the hint text color gray | |
tv.setTextColor(Color.GRAY); | |
} | |
else { | |
tv.setTextColor(Color.BLACK); | |
} | |
return view; | |
} | |
}; | |
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item); | |
spinner1.setAdapter(spinnerArrayAdapter); | |
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
String selectedItemText = (String) parent.getItemAtPosition(position); | |
// If user change the default selection | |
// First item is disable and it is used for hint | |
if(position > 0){ | |
// Notify the selected item text | |
Toast.makeText | |
(getApplicationContext(), "Selected : " + selectedItemText, Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment