setBackgroundResource() to spinner in Android 5 not works

966 Views Asked by At

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;
    }

}
4

There are 4 best solutions below

4
On

The solution for this is to add this code when creating the spinner dynamically:

spinner.setPopupBackgroundResource(R.drawable.spinner);

and to create spinner.xml under Drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>

This solution require API level of 16 and above.

7
On

Give try to this code:

spinner.setBackgroundColor(Color.parseColor("#FFFF00"));

"#FFFF00" instead of this use any other color code.This is working for me in Android v5.

2
On

In Android Lollipop Version you have to implement code for kitkat version and lollipop version separately , please do this code for change background of spinner. its example of image background changes of images.

        if (Build.VERSION.SDK_INT => Build.VERSION_CODES.KitKat) 
                        {
                            //for Lollipop Vession

//set color in this block which you want 
spinner.setBackgroundDrawable(Color.RED);
                        }
                        else
                        {
    // for Kikat Version

spinner.setBackgroundResource(xyz);
//set color in this block which you want 
                        }

i hope it helps you, if it is useful code for then please mark me .. Thanx.. :)

0
On

Have you set any adapter to your spinner? if not use something like this: Place in your activity's onCreate() method:

//Get your spinner
spinner = (Spinner)findViewById(R.id.spinner);

// Defined Array values to show in ListView
String[] values = new String[] { "item1",
            "item2",
            "item3"};

//Construct the ArrayAdapter
ArrayAdapter<String> spinneradapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, values);

 //Set the adapter to the corresponding spinner
spinner.setAdapter(spinneradapter);

 //Change background color to "RED"
 spinner.setBackgroundColor(Color.parseColor("#FFFF0000"));

Tested, Works with android 5.0.1

Hope this will help you out