I have to change the color of spinner first element which is displayed as the default element. Tried various methods not working. provide a working solution.
-
you need to set custom spinner_item layout . show what you tried – Usman Rana Jul 06 '17 at 11:42
-
@ShivanshSaxena Whatever you have tried or what is now are you trying include that code. – Nitin Patel Jul 06 '17 at 11:56
-
follow this link: https://stackoverflow.com/questions/44513126/spinner-background-color-with- dropdowndown-icon/44513290#44513290 – Sarvesh Verma Jul 06 '17 at 12:26
-
check this answer please: https://stackoverflow.com/questions/5836254/android-change-text-color-of-items-in-spinner – ysfcyln Nov 20 '17 at 13:21
3 Answers
I checked this solution after some modifications, this answer will helps you to change first spinner
of item.
In onCreate, calling common method as setSpinnerValue
:
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
setSpinnerValue(testarray,timeSpinner,arrayAdapter);
Here, i have created Common method with arrayList
, spinner
and arrayAdapter
as parameter for setting spinner items in adapter
.
public void setSpinnerValue(final ArrayList<String> list, Spinner spinner, ArrayAdapter<String> adapter)
{
try {
adapter = new ArrayAdapter<String>(TimePicker_Activity.this, android.R.layout.simple_spinner_item, list) {
@Override
public boolean isEnabled(int position) {
String val = String.valueOf(list.get(position));
boolean isFalse = val.contains(":False");
return !isFalse;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
TextView tv = (TextView) v.findViewById(android.R.id.text1);
String val = String.valueOf(list.get(position));
tv.setText(val.replace(":False", ""));
switch (position) {
case 0:
tv.setTextColor(android.graphics.Color.GRAY);
break;
default:
tv.setTextColor(getResources().getColor(R.color.colorPrimary));
break;
}
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setSelection(0, false);
spinner.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
Getting output as below,

- 1,420
- 4
- 13
- 23
For the people who struggle to get this working, this is the Kotlin solution.
Subclass the
ArrayAdapter
classclass CustomArrayAdapter(context: Context, resource: Int, objects: List<String>) : ArrayAdapter<String>(context, resource, objects) { override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View { val view = super.getDropDownView(position, convertView, parent) if (view is TextView) { if (position == 0) { view.setTextColor(ContextCompat.getColor(parent.context, R.color.red_color_1)) } else { view.setTextColor(ContextCompat.getColor(parent.context, R.color.blue_color_1)) } } return view } }
Connect to adapter to your list:
fun setAdapter() {
CustomArrayAdapter(this, R.layout.custom_list_item, buildItems()).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(R.layout.custom_list_item)
// Apply the adapter to the spinner
binding.mySpinner.adapter = adapter
}
}
Keep in mind that the list is a RecyclerView
. It means, essentially, that if you change one of the items color, you gotta change the color to all of the other items also (if the position == 0 AND if the position != 0, cause the position == 0 item will be used again and again)

- 1,434
- 17
- 16
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
if (position == 0) {
(view as TextView).setTextColor(resources.getColor(R.color.colorgray))
}
try to set position of zero on itemSelected overriden function ..

- 51
- 3