6

I'm creating a SpinnerAdapter using the built in resource ID android.R.layout.simple_spinner_dropdown_item

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item);

but the textView which that resource ID creates has textColor Black and I need a different color. What's the best way of changing the color but keeping everything else the same?

When I try and create my own textView layout resource in an xml file, e.g.

<?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:textColor="@color/White"
    />

then it doesn't behave in the same way as android.R.layout.simple_spinner_dropdown_item because e.g. the padding is different. So

  1. Is there a way of creating my own layout resource in an xml file which inherits everything from android.R.layout.simple_spinner_dropdown_item and allows me to override the textColor?
  2. Or is the complete definition of android.R.layout.simple_spinner_dropdown_item available somewhere?
  3. Or perhaps there's an even easier way somehow?

This question relates to another question that I've asked today (Can't change the text color with Android Action Bar drop-down navigation). I've realised that one answer to that question (and hence this question) is to create my own ArrayAdapter class which inherits from ArrayAdapter<T> so that I can always set the color in code whenever the ArrayAdapter gets used (see my answer to that question). But that seems like a very cumbersome solution :-|.

Changing a text color shouldn't be a hard task!

Community
  • 1
  • 1
Stochastically
  • 7,616
  • 5
  • 30
  • 58

3 Answers3

9

Is there a way of creating my own layout resource in an xml file which inherits everything from android.R.layout.simple_spinner_dropdown_item and allows me to override the textColor?

No, but you can cut & paste your favorite version of simple_spinner_dropdown_item.xml to a new file then simply change the text color.

Or is the complete definition of android.R.layout.simple_spinner_dropdown_item available somewhere?

Yes, but there are many different versions. Which API do you like? Also you might have a copy on your hard drive already, check <android-sdk>/platforms/android-xx/data/res/layout/ (where xx is a particular API.)


As an alternate approach you can create a custom Adapter and change the text color after you inflate each row, but the method above will be slightly faster since it doesn't involve any run time changes.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • thx vm :-). In fact I'd already found your alternate approach, but now you've told me where to look for the xml defintions I'll probably switch to that. – Stochastically Apr 11 '13 at 18:29
  • +1 for your line of thinking...I added to your solution since this won't work right away without changes. – whyoz Jul 11 '13 at 22:16
9

To expand on Sam's solution, you will need to modify the file in very specific ways to override Android's styles.

After you copy the simple_spinner_dropdown_item.xml from the sdk folder into the correct layout folder you are working with, you will see this code:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />

In order to change the background of the TextView and avoid this error:

.../res/layout/simple_spinner_dropdown_item.xml:20: error: Error: Attribute is not public. (at 'layout_height' with value '?android:attr/dropdownListPreferredItemHeight').

your code will need to look like this:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/spinnerText"
style="@style/SpinnerTextViewItem"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

The other main change that needs to be made is to create your own style. You probably have a styles.xml file, so add a new style similar to this:

<style name="SpinnerTextViewItem" parent="@android:style/Widget.TextView" >
    <item name="android:textStyle">normal</item>
    <item name="android:background">@color/green</item>
</style>

Assuming you have something in your colors.xml file, you can set the color via "@color/green" or whatever color you choose (of course you can use @android:color/...) not to mention all the other items you can add to change the TextView.

whyoz
  • 5,168
  • 47
  • 53
0

Without using custom adapter it is impossible to change the color of

 android.R.layout.simple_spinner_dropdown_item

Better create a custom adapter and then you can change the color, testSixe, textStyle, etc.

Parijat Bose
  • 380
  • 1
  • 6
  • 22
  • well, if you do what Sam says to do, then you are able to edit simple_spinner_dropdown_item.xml... – whyoz Jul 11 '13 at 21:59