Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android spinner custom style for spinnerDropDownItemStyle not working

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.

like image 688
Prashant Avatar asked Dec 10 '22 21:12

Prashant


2 Answers

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.

like image 85
Rafael Chagas Avatar answered May 16 '23 08:05

Rafael Chagas


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);
like image 27
Riga Avatar answered May 16 '23 07:05

Riga