fine line
fine line

Reputation: 185

change Spinner text color

I want to change the default text color in Spinner to any other color. But I have not find the solution yet. All I got a solution through TextView like How to change spinner text size and text color?
Is there any other way, direct way for Spinner?

Note: Please provide solution for Xamarin. Thank you.

Upvotes: 3

Views: 27747

Answers (5)

Raihan Mahamud
Raihan Mahamud

Reputation: 63

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_5"
android:ellipsize="marquee"
android:layoutDirection="ltr"
android:padding="@dimen/padding_5"
android:singleLine="true"
android:textSize="@dimen/textsize_base" />




adapter?.setDropDownViewResource(R.layout.spinner_text)

spSourceAccount.adapter = adapter

Upvotes: 0

user8427383
user8427383

Reputation:

create a Sppinner code

<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>

You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />

If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:

 <?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>

And finally another change in the declaration of the spinner:

 ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
 R.array.planets_array, R.layout.spinner_item);
 adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);  
 spinner.setAdapter(adapter);

Upvotes: 2

Aanchal Garg
Aanchal Garg

Reputation: 12

you can do it by adding below code to style.xml:

<resources>
<style name="MySpinnerLook" 
    parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:textSize">22sp</item>    //any size
</style>

and in the xml where spinner is used add refrence to MyStyleLook using style tag like:

style="@style/MySpinnerLook"

Upvotes: 0

Mani Vasagam
Mani Vasagam

Reputation: 820

Try like this only for selected item color change:

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
}
});

Another way:

<style name="spinnerTheme">
<item name="android:textColor">@color/gray_dark</item>
</style>

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:theme="@style/spinnerTheme"/>

Upvotes: 21

Ayan
Ayan

Reputation: 1

Create a new XML layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#962b2b"
android:textAlignment="inherit"/>

Now in the set the spinner adapter with the custom XML layout like this:

ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.custom_spinner,array);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(adapter);

Upvotes: 0

Related Questions