Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text color in spinner without using textview

I want to change text color for spinner without using textView, I have already searched and found some tutorials Android: Where is the Spinner widget's text color attribute hiding?

but the main thing is they have used textView.

<Spinner 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:entries="@array/Gender_Selection_arrays" 
     android:prompt="@string/Gender_Selection"
     android:id="@+id/gendersel"
     android:popupBackground="#67656c"/>

I know this code is not to change text color.

I have no idea how to do that,please guide me for doing the same.

Any help would be appreciable.

like image 355
Ashish Jaiswal Avatar asked Jul 20 '15 11:07

Ashish Jaiswal


2 Answers

I got solution for this, Create a new xml "spinner_style.xml"

<?xml version="1.0" encoding="UTF-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="@color/Black" />

And fix this layout as your spinner style in your activity:

   ArrayAdapter<String > gender_adapter = new ArrayAdapter<String> (getActivity(), R.layout.spinner_style,gender_spinner );
like image 120
Ashish Jaiswal Avatar answered Sep 21 '22 10:09

Ashish Jaiswal


Your Spinner

<Spinner 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:entries="@array/Gender_Selection_arrays" 
    android:prompt="@string/Gender_Selection"
    android:id="@+id/gendersel"
    style="@style/mySpinnerItemStyle"
    android:popupBackground="#67656c"/>

mySpinnerItemStyle (Add this to styles.xml)

<style name="mySpinnerItemStyle" parent="Base.Widget.AppCompat.Spinner">
    <item name="android:textColor">@color/my_spinner_text_color</item>
</style>

And finally in colors.xml

<color name="my_spinner_text_color">#FFFFFF</color>

mySpinnerItemStyle inherits from Base.Widget.AppCompat.Spinner and in that android:textColor attribute changes the color of the spinner text

like image 35
capt.swag Avatar answered Sep 20 '22 10:09

capt.swag