strange things happen. I am trying to set background to spinner. spinner.setBackgroundResource(R.drawable.spinner_error);
This code works perfect in lower Android versions than 5, but in lollipop, this code do nothing. I tried to change color spinner.setBackgroundColor(Color.RED);
but it only makes spinner invisible. So, how to change spinner color/resource ?
EDIT: Adapter code:
public class FuelStoragesAdapter extends ArrayAdapter<ModelFuelStorage> {
private Context context;
private List<ModelFuelStorage> fuelStorages;
private boolean addAdditionalItems = false;
public FuelStoragesAdapter(Context context, List<ModelFuelStorage> fuelStorages, boolean addAdditionalItems) {
super(context, android.R.layout.simple_spinner_item, fuelStorages);
this.context = context;
this.fuelStorages = fuelStorages;
this.addAdditionalItems = addAdditionalItems;
setDropDownViewResource(R.layout.spinner_dropdown_layout);
if (addAdditionalItems)
addAdditionalItems();
}
public void addStorage(ModelFuelStorage fuelStorage) {
insert(fuelStorage, getCount() - 1);
notifyDataSetChanged();
}
public void addAdditionalItems() {
fuelStorages.add(new ModelFuelStorage(0, context.getString(R.string.add_new_storage), 0));
fuelStorages.add(new ModelFuelStorage(-1, context.getString(R.string.choose_storage), 0));
}
@Override
public int getCount() {
if (addAdditionalItems)
return super.getCount() - 1; // you dont display last item. It is used as hint.
else
return super.getCount();
}
public int getPosition(int storageId){
for(ModelFuelStorage storage : fuelStorages)
if(storage.getId() == storageId)
return fuelStorages.indexOf(storage);
return 0;
}
}
The solution for this is to add this code when creating the spinner dynamically:
and to create spinner.xml under Drawable folder:
This solution require API level of 16 and above.