Reputation: 1121
PROBLEM
I've tried to change the select highlight in my application but with no luck. I've been doing it via styles because I have quite a lot of them in my app. I would be grateful if you have told me what is wrong with my code.
CODE
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionOverflowButtonStyle">@style/MyActionBar</item>
<item name="android:imageButtonStyle">@style/MyImgBtn</item>
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
<item name="android:spinnerStyle">@style/MySpinnerTheme</item>
<item name="android:windowBackground">@android:color/white</item>
</style>
<style name="MySpinnerTheme" parent="android:Widget.Holo.Light.Spinner">
<item name="android:activatedBackgroundIndicator">@drawable/custom_activated_background</item>
</style>
UPDATE
So I've managed to merge two selectors with setting style on setDropDownViewResource
layout element. But what I get at the moment are two selectors appearing at the same time.
I've tried to set android:dropDownSelector="@android:color/transparent"
on Spinner in XML but still no luck. Posting more code below.
SPINNER
final Spinner yearSpinner = (Spinner) rootView.findViewById(R.id.yearSpinner);
ArrayAdapter<SpinnerItem> adapterYear = new ArrayAdapter<SpinnerItem>(getActivity(),
R.layout.spinner_item_layout , yearsItems);
yearSpinner.setOnItemSelectedListener(itemSelectedListener);
yearSpinner.setAdapter(adapterYear);
<Spinner
android:id="@+id/yearSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownSelector="@android:color/transparent"
android:layout_below="@id/yearTxt"
android:layout_marginLeft="20dp"
android:popupBackground="@drawable/podpowiedzi"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
>
</Spinner>
SPINNER_ITEM_LAYOUT
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:textStyle="italic"
android:textAlignment="inherit"
android:textColor="@color/text_color"
>
</TextView>
Upvotes: 0
Views: 2918
Reputation: 776
To change the style and colours of your spinner drop down items add the following to your style.xml
<style name="mySpinnerItemStyle" parent="@style/android:Theme.Holo">
<item name="android:background">@drawable/spinner_selector</item>
<item name="android:gravity">center_vertical</item>
</style>
And then create the "spinner_selector.xml" in your drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@color/orange" /> <!-- current selected -->
<item android:state_pressed="true" android:drawable="@color/orange_bright" />
<item android:drawable="@android:color/transparent" />
</selector>
If you want to custom highlight colour when you press on the spinner, we need 9 patch images for spinner backgrounds. Do the following steps:
1) visit this website:http://android-holo-colors.com/, select Spinner, select the colour you want for your spinner highlight and download the zip file. (there are many other options depends on your app)
2) In the zip file open res-->drawable and save file "apptheme_spinner_background_holo_light.xml" to your drawable folder
3) save the following images in the right drawable folders: apptheme_spinner_default_holo_light.9.png apptheme_spinner_disabled_holo_light.9.png apptheme_spinner_focused_holo_light.9.png apptheme_spinner_pressed_holo_light.9.png
4) add this to your style.xml file:
<style name="MySpinnerTheme" parent="@android:Widget.Holo.Light.Spinner">
<item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
</style>
In future, you can auto generate any style you want with all the resources required (including spinner highlight colour) thr this website: http://android-holo-colors.com/
Upvotes: 2