Search code examples
androidspinner

How to change spinner text size and text color?


In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.

Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>  (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();

list.add("All Lists");

if (cursor.getCount() > 0) {
    for (int i = 0; i < cursor.getCount(); i++) {
        keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
        list.add(keyList[i]);
        cursor.moveToNext();
    }
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.

android:textColor="@android:color/white"
android:textSize="11dp"

How can I change the text color and text size of my spinner?


Solution

  • Make a custom XML file for your spinner item.

    spinner_item.xml:

    Give your customized color and size to text in this file.

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="left"  
        android:textColor="#FF0000"         
        android:padding="5dip"
        />
    

    Now use this file to show your spinner items like:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
    

    You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.