0

I have managed to change the spinner text and background color using the code below.

spinner_layout.xml layout file

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textColor="@color/colorBlack"
    android:textSize="18sp"
    android:background="@color/colorWhite"
    android:fontFamily="@font/raleway"/>

kt file calling the above

ArrayAdapter(requireContext(), R.layout.spinner_layout, companyList)

The above creates a black background with white text when the spinner is open but on close, my text is black on black as my layout background is black too. How do I change the spinner close state text to white?

2 Answers2

1

You can have different layout on open and on close by using this.

 adapter.setDropDownViewResource(R.layout.your_layout_resource_xml)

Of course before you have to create custom adapter, but i guess you already did that.

To have best result about the background, are need more changes:

how can i change spinner background color?

Basically you create custom layout for spinner and spinner items with two different layout

  <style name="AppTheme.spinnerStyle" 
    parent="@android:style/Widget.Material.Light.Spinner"> 

     <item name="android:textColor">@android:color/white</item>
     <item name="android:background">@color/colorPrimary</item>

  </style> 
  <style name="AppTheme.spinnerDropDownItemStyle" 
    parent="@android:style/Widget.Material.DropDownItem.Spinner">

    <item name="android:textColor">@android:color/white</item> 
    <item name="android:background">@color/colorPrimary</item>
  </style>
Dak28
  • 259
  • 1
  • 8
0

Make your spinner like this

 <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="20dp"
                android:background="@color/black"
                android:minHeight="45dp"
                android:padding="3dp">

                <Spinner
                    android:id="@+id/girth"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                     />
            </RelativeLayout>


And your spinner item view layout like this

<?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:padding="5dp"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:fontFamily="@font/raleway"/>


And in your onItemeSelectedListener change selected text color like this

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    ((TextView)view).setTextColor(Color.WHITE);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}

});

Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22