Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the items' text color in Spinner Android


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.! =)

like image 254
Preeyah Avatar asked Sep 21 '12 05:09

Preeyah


People also ask

How do you color text on Android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How to set textSize in Spinner?

In summary to change the text size (or other style attributes) for a Spinner either: Create a custom TextView layout. Change the text size with the android:textSize attribute. Change the text color with android:textColor.

How do I add color to TextView?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.


2 Answers

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/?

like image 29
The Holy Coder Avatar answered Sep 20 '22 20:09

The Holy Coder


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);
like image 104
V.J. Avatar answered Sep 17 '22 20:09

V.J.