How to set the items' text color in Spinner Android

21,112

Solution 1

Use Custom ArrayAdapter or Custom Spinner.

Have you seen http://www.coderzheaven.com/2011/02/20/how-to-create-custom-layout-for-your-spinner-in-android-or-customizable-spinner-2/?

Solution 2

Create a xml file for your spinner item. and put it in layout folder

spinner_view.xml:


<TextView  
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:gravity="left"  
  android:textColor="@color/green"         
/>

and finally in your code.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_view,yourList);
Share:
21,112
Preeyah
Author by

Preeyah

Updated on May 15, 2020

Comments

  • Preeyah
    Preeyah almost 4 years


    I'm having problems with setting text color for the Spinner. I've seen few examples but most have ArrayAdapter and String array from strings.xml in res folder as my Spinner's items are retrieved from SQLite Database so I think it may not help.

    Here are my Spinner's codes PersonalInformation.java

    public class PersonalInformation extends Activity
    {
        EditText txtLikes, txtDislikes, txtType, txtDate;
        Button btnView, btnBack;
        Spinner nameSpinner;    
    
        final Context context = this;
    
        private int namesSpinnerId;        
    
        LikesDBAdapter likeDB = new LikesDBAdapter(this);
        DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this);
    
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.info);
    
            BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
            buddyDB.open();
    
            Cursor friendsCursor = buddyDB.getAllNames();
            startManagingCursor(friendsCursor); 
    
            String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
            int[] to = new int[]{R.id.name};
    
            SimpleCursorAdapter friendsCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, friendsCursor, from, to);
            friendsCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            nameSpinner = (Spinner)findViewById(R.id.nameSpinner);
            nameSpinner.setAdapter(friendsCursorAdapter);
            //buddyDB.close();
    
    
            nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
                {
                     @Override
                     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                     {
                         Cursor c = (Cursor)parent.getItemAtPosition(pos);
                         namesSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
                     }
    
                    @Override
                    public void onNothingSelected(AdapterView<?> parent)
                    {
                        // TODO Auto-generated method stub
    
                    }
                });
            buddyDB.close();
    

    And, these are Spinner's layout codes in info.xml

    <Spinner
            style="@style/SpinnerStyle"
            android:id="@+id/nameSpinner"        
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:prompt="@string/friends_prompt"
            android:textColor="@color/green" />
    

    Please help me with how to set the items text color in Spinner.
    I'll appreciate any help provided. Thanks.! =)