shruti lv
shruti lv

Reputation: 25

How to change Spinner text colour?

I have issue in changing Spinner text colour. I am using the following code to change Text Colour. But I don't know why it's throwing a NullPointerException at setTextColor(). I am fetching items of the spinner from a database dynamically.

 public class PersonalInformation extends Activity
 {

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info)
    ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_spinner_item,arrayList);
    adapter8.setDropDownViewResource(R.layout.spinner_view);
    mySpinner.setAdapter(adapter8);

    mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {
         @Override
         public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
         {

           ((TextView) arg0.getChildAt(arg2))
                            .setTextColor(Color.BLACK);
                    onulocation = arg0.getItemAtPosition(arg2)
                            .toString();
         }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // TODO Auto-generated method stub

        }
    });
}

Upvotes: 0

Views: 130

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

this may help you...

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            TextView textView = (TextView) view;
            textView.setTextColor(Color.BLACK);
            onulocation = parent.getItemAtPosition(pos).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

Upvotes: 1

Related Questions