tfh
tfh

Reputation: 620

Android change text color in a Spinner

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...

Upvotes: 3

Views: 13617

Answers (5)

Hanisha
Hanisha

Reputation: 887

To change the text color .

 ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);

Upvotes: 0

tfh
tfh

Reputation: 620

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.

Upvotes: 0

hw4m
hw4m

Reputation: 432

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>

Upvotes: 5

Ramkumar.M
Ramkumar.M

Reputation: 691

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);

Upvotes: 4

Pradeep Gupta
Pradeep Gupta

Reputation: 1770

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);

Upvotes: 0

Related Questions