How to change spinner text color

后端 未结 8 1988
抹茶落季
抹茶落季 2020-12-10 02:50

I saw many topics about how to change spinner\'s text color,but i couldnt understand how to use

spinner_item.xml



        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 03:27

    From the API level 16 and above, you can use following code to change the drop down icon in spinner. just goto onItemSelected in setonItemSelectedListener and change the drawable of textview selected like this.

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView parent, View view, int position, long id) {
          // give the color which ever you want to give to spinner item in this line of code
    
        //API Level 16 and above only.
                ((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null);
    
    //Basically itis changing the drawable of textview, we have change the textview left drawable.
                }
                @Override
                public void onNothingSelected(AdapterView parent) {
               }
            });
    

    hope it will help somebody.

提交回复
热议问题