miatech
miatech

Reputation: 2268

Changing the Color of a Spinner

I have a spinner that I want to change the color when it is not clicked. the view in the activity. I managed to change the color of text and background when it is clicked, but not the default text and background. and because the background of the app is dark it is difficult to see. How can I do this. this is my spinner code on activity_main.xml

<Spinner
        android:id="@+id/spinner"
        android:layout_width="114dp"
        android:layout_height="33dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:theme="@style/Spinner"
        app:layout_constraintBottom_toTopOf="@+id/ping_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toEndOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.025" />

my style for spinner

<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
        <item name="android:textColor">@color/textColor</item>
        <item name="android:backgroundTint">@color/background</item>
        <item name="android:textSize">14sp</item>
    </style>

here's a screenshot of the app http://pctechtips.org/apps/droid.png

Upvotes: 1

Views: 332

Answers (1)

Peer Mohamed
Peer Mohamed

Reputation: 448

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.

Reference from 'How to change spinner text size and text color?'

Upvotes: 2

Related Questions