Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the color of the textView android.R.layout.simple_spinner_dropdown_item?

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!

like image 960
Stochastically Avatar asked Apr 11 '13 18:04

Stochastically


People also ask

How to change Color of TextView in android?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.

How to change TextView Color in android dynamically?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


2 Answers

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.

like image 114
Sam Avatar answered Nov 10 '22 01:11

Sam


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.

like image 22
whyoz Avatar answered Nov 10 '22 01:11

whyoz