0

I am using a spinner in my application and setting its item values via xml. I have set spinner item default color via style, I have to enable and disable my spinner as required in my application. I want to change its item color programmatically when I set its state disable. I have searched through google but could not find any solution for this.Is there anyone who has any idea about this how can I set it.I have used this link how do you set the spinner text color?, but it is giving null pointer exception.

    m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22

1 Answers1

1

You can create custom layout for your spinner item as like created here :

How to change spinner text size and text color?

and use it inside your ArrayAdapter.

Now, As per your requirement you have to create two xml files to provide two different colors to spinner items :

1) When your Spinner is Enabled.

2) When your Spinner is Disabled.

Just change the line below as :

if(spinner1.isEnabled){
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item1,list);
}else{
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item2,list);
}
Community
  • 1
  • 1
user5716019
  • 598
  • 4
  • 17
  • thanks for reply in my case this can be hectic because I am using a table layout in my app and there are more than 5 spinner in a row and I have set to set it for all those . – Sandeep dhiman Feb 11 '16 at 09:20