Android change text color in a Spinner
Solution 1
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.
Solution 2
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>
Solution 3
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);
Comments
-
tfh almost 2 years
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 about 8 yearsI 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 about 8 yearsI 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 about 8 yearsThis 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.
-
oiZo about 8 yearsYou should probably include the style in your original question then, maybe it's something simple thats wrong with the your xml.