I have added some custom style to Android spinner. I am trying to have prompt small but drop down items little big. My code looks like below :
theme.xml
<style name="Theme.MyApp" parent="@android:style/Theme.Holo.Light.DarkActionBar">
.
.
.
<item name="android:spinnerItemStyle">@style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">@style/spinnerDropDownItemStyle</item>
</style>
.
.
.
<style name="spinnerItemStyle">
<item name="android:padding">10dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">#000000</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">20dp</item>
<item name="android:textSize">30sp</item>
<item name="android:textColor">#000000</item>
</style>
Now I am able to set the spinnerItemStyle
properly, but some how style for spinnerDropDownItemStyle
is not having any effect, its not working. Any clues why is this happening ? I want my drop-down items with big text size than that of prompt item.
This question is old but was not correct answered.
Support Library has a custom support_simple_spinner_dropdown_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
Therefore, to make your custom style work, you must override a local spinnerDropDownItemStyle (without the android: prefix) in your app style.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:spinnerItemStyle">@style/TextViewSpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/TextViewSpinnerDropDownItem</item>
<!-- Override the dropdown item in support library -->
<item name="spinnerDropDownItemStyle">@style/TextViewSpinnerDropDownItem</item>
</style>
Change whatever you want:
<style name="TextViewSpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
</style>
<style name="TextViewSpinnerDropDownItem" parent="Widget.AppCompat.DropDownItem.Spinner">
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
</style>
Now, all your simple spinner dropdowns will look exactly the same.
Found this article that explains how to do it: How to change a Spinner text size, color or overall style
You need to create a style for the input item and the dropdown items and add them.
Customize spinner input:
<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" />
Add style:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);
Customize dropdown list items:
<?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:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
Add style to spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With