3

I have a Spinner and use an ArrayAdapter. In the adapter I use "android.R.layout.simple_list_item_1", like this:

spinnerControlObjectType.setAdapter(new ArrayAdapter(getApplicationContext, android.R.layout.simple_list_item_1, list))

I looked in android.R.layout.simple_list_item_1 and see the it has a text styling like this:

android:textAppearance="?android:attr/textAppearanceListItemSmall"

I want to overwrite "textAppearanceListItemSmall" in my theme in order to give it a different color, how can I do that? I do not want to subclass anything or write boilerplate of code. I am sure there is a way to change the color only changing the theme.xml.

In the android docs it is written: '...Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."...' (http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes). They say "defined" and "in the current theme" - how can I define it in my current theme? Makes me nuts...

tfh
  • 620
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [How to change spinner text size and text color?](https://stackoverflow.com/questions/9476665/how-to-change-spinner-text-size-and-text-color) – Md Imran Choudhury Dec 10 '17 at 08:57

5 Answers5

5

You should just override that attribute in your theme, in this example i'm using AppCompat theme as parent, but you can change that to any other theme. Depending on what you want, you should make themes and styles resources for different versions of Android:

<style name="MyTheme" parent="Theme.AppCompat.Light">
    <item name="android:textAppearanceListItemSmall">@style/MySpinnerStyle</item>
</style>

<style name="MySpinnerStyle" parent="TextAppearance.AppCompat.Subhead">
    <item name="android:textSize">13sp</item>
    <item name="android:textColor">@color/white</item>
</style>
oiZo
  • 432
  • 3
  • 10
  • This is exactly what I am searching for - I have basically this in my code but it just does not work. But I compare mine with yours again for a second. – tfh Apr 15 '16 at 13:14
  • You should probably include the style in your original question then, maybe it's something simple thats wrong with the your xml. – oiZo Apr 15 '16 at 13:16
4

Make a custom XML file for your spinner item

spinner_layout.xml

add customized color

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_spinner"
    android:textSize="16sp"
    android:text="HELLO"
    android:padding="10dp"
    android:textColor="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

use this file to show your spinner items

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_layout,ar);
        mSpinner.setAdapter(adapter);
Ramkumar.M
  • 681
  • 6
  • 21
  • 2
    I am aware of that. But for me it is not acceptable to add a new xml resource file for only changing a color. I want to have the color change by ONLY changing stuff in my theme.xml. Thanks anyway! – tfh Apr 15 '16 at 13:11
0

try this,

        // Initializing an ArrayAdapter
        final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this,R.layout.spinner_item,plantsList){
            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;

                    // Set the Text color
                    tv.setTextColor(Color.parseColor("#FFC9A3FF"));

                return view;
            }
        };
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
        spinner.setAdapter(spinnerArrayAdapter);
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
  • I only want to change a style in my theme for changing the color. This is just to much work for a simple color change. Thanks anyway, Pradeep! – tfh Apr 15 '16 at 12:56
0

I answer my own question - all works as the documentation says. My issue was that I wanted to use "android.R.layout.simple_list_item_1" in a Spinner component. As soon as I used "android.R.layout.simple_spinner_item" it worked.

tfh
  • 620
  • 1
  • 4
  • 14
0

To change the text color .

 ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
Hanisha
  • 849
  • 10
  • 8