0

I have the following spinner xml code:

  <Spinner
    android:id="@+id/spinnerMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_weight="0.40"
    android:textColor="@color/black"
    android:background="@drawable/rounded_white_rectangle"/>

and the following java code:

public void setMainCategories(String[] mainCategories) {

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mainCategories);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerMain.setAdapter(spinnerArrayAdapter);

}

@Override
public void setSubcategories(String[] subCategories) {
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, subCategories);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerSub.setAdapter(spinnerArrayAdapter);

and I'm not sure how to change the java code for the text to change color.

Do you have any suggestions? Would greatly appreciate it.

ti3kto3k
  • 1
  • 3

2 Answers2

1

You can change the Appearance of Dropdown Items in the Spinner via Styles:

Add a style XML and use the in-built Android attribute:

<style name="mystyle">
    <item name="android:spinnerDropDownItemStyle">@style/spinner_drop_item_style</item>
</style>
<style name="spinner_drop_item_style">
    <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:gravity">center_vertical|start</item>
    <item name="android:orientation">vertical</item>
    <item name="android:background">@drawable/apptheme_list_selector</item>
</style>

Depending on your usecase a custom View like @Jhaman proposed might be the easier solution.

EDIT: You have also apply the Style somehow to your view or Application. The documentation is good in explaining this, but all in all the Style system and the various attributes are not that well described.

J. Dow
  • 563
  • 3
  • 13
0

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);

Hope it will helps you :)

Jhaman Das
  • 1,094
  • 13
  • 28