Android Change text color of Spinner (display text - no items in spinner)
Except for one screen, Spinner
must have "black" text. One screen requires Spinner
a "white" color (both display text for selected item and counter items).
Here is a picture for better clarity.
I can get "white" color for the counter items, but not for the selected display text. Again, I don't want to globally change the style - just this screen. Is it possible?
source to share
When you create a Spinner, you must provide an adapter for it. If you used the default implementation from Android developers:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
What you can do is create your own layout, which is to represent the selected text of the element, which is provided as and placed inside the adapter, and there you can provide a textColor attribute, for example:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="@color/primary_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
If you are using ArrayAdapter.createFromResource()
, remember to have a TextView with android:id="@android:id/text1"
so that this adapter can bind data to your layout.
source to share