1

I have a spinner inside a layout.

<Spinner android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:spinnerMode="dropdown"
    android:id="@+id/spn_college_names"></Spinner>

I am setting it's value using Matrix Cursor and Simple Cursor Adapter as below spn_CollegeNames = (Spinner) findViewById(R.id.spn_college_names);

    String[] ColoumnNames = {"_id","CollegeName"};
    _CollegeNames = getResources().getStringArray(R.array.settings_college_names);
    MatrixCursor cursor = new MatrixCursor(ColoumnNames);

    for (int CollegeIndex = 0; CollegeIndex < _CollegeNames.length; CollegeIndex++ )
    {
        cursor.addRow(new Object[] {CollegeIndex+1 , _CollegeNames[CollegeIndex]});
    }

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 );
    /*TextView tv = (TextView) spn_CollegeNames.getSelectedView();
    tv.setTextColor(Color.BLACK);*/
    spn_CollegeNames.setAdapter(cursorAdapter);

My problem now is that the text in the spinner is appearing in gray color. I want to change the text to black. I know I can use android:enteries attribute in the layout but I will be replacing the matrix cursor with database cursor in future which means I will not have fixed string array values.

Akhila
  • 141
  • 1
  • 9

3 Answers3

3

You can achieve it by overriding getDropdownView and/or getView. getDropdownView returns view for expanded spinner and getView returns view for collapsed spinner.

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 ){

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View view = super.getDropDownView(position,convertView,parent);
    TextView text = (TextView)view.findViewById(android.R.id.text1);
    text.setTextColor(Color.BLACK);
    return view;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position,convertView,parent);
    TextView text = (TextView)view.findViewById(android.R.id.text1);
    text.setTextColor(Color.BLACK);
    return view;
}

};
Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63
1

Try this :

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
    }

    public void onNothingSelected(AdapterView<?> parent) {
    }
};
Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
  • Thank you @Virag It changes the color of the text of the selected item do you have a solution to change the color of all the values in drop down. – Akhila Dec 04 '13 at 07:47
  • Thank you but that prevents me from using cursor. – Akhila Dec 04 '13 at 07:57
  • 1
    Instead of android.R.layout.simple_spinner_dropdown_item you have to provide your custom textView item – Virag Brahme Dec 04 '13 at 07:57
  • I know about that solution but I was checking if there is a way to avoid using custom textview. Any way thanks for confirming. – Akhila Dec 04 '13 at 08:09
1

Instead of android.R.layout.simple_spinner_dropdown_item you have to provide your custom textView item .

Spinnerlayout.xml

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00eeff" 
android:text="TextView" />

Your adapter

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.spinnerlayout, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 );
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64