Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner item pressed blue background

I use a spinner to display a dropdown list. I want to have the items in the list to have rounded corners. So I use 9-patch containing an image with rounded corners (transparent on the outside of the corners) for the views items background and a selector to display a different color 9-patch when pressed.

Problem: When I press the items in the spinner's list, I can see a blue background in the corners, where the 9-patch is transparent.

I can't seem to get rid of this blue background that appears when the items are pressed. If I remove the 9-patches and any setting in the spinner, I can see that the items views in the list are by default grey, and blue when pressed.

I tried also to not use 9 patches as the background but just a color selector, and set the pressed color in the selector to be transparent. Then when I press the item, it's not transparent, but blueish. I think the view in the list is really transparent, but there is still the blue color in the background when pressed...

I use a custom SpinnerAdapter to create the items view. Here is the simplified code:

   private class MySpinnerAdapter implements SpinnerAdapter {
        @Override
        public View getDropDownView(int i, View recycledView, ViewGroup viewGroup) {
            View view = new View(context);
            view.setBackground(context.getResources().getDrawable(R.drawable.testspinner));
            view.setMinimumHeight(100);
            return (View) view;
        }
} 

The selector used for the background. Here with just a color, and no 9-patch. The pressed color should be transparent:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:state_pressed="true"
            android:drawable="@android:color/transparent" />
    <item
            android:drawable="@android:color/holo_purple" />

</selector>

I set the custom adapter on the spinner:

    spinner.setAdapter(new MySpinnerAdapter());

And the spinner is fetched from XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="wrap_content">

<Spinner
        android:id="@+id/myDropDown"
        android:spinnerMode="dropdown"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:dropDownWidth="match_parent"/>
</LinearLayout>

I have tried setting many different attributes on the Spinner, and tried some styling attributes, but I could not get rid of this blue background...

like image 733
gpo Avatar asked Mar 11 '14 10:03

gpo


2 Answers

Thank you @Vikram for pointing out the use of styles.

However, after investigations it turns out that the background of the pressed item in the Spinner doesn't come from the attribute you suggested, but from android:listSelector. So I could fix the problem like this:

Define a new style in styles.xml:

<style name="MyListView">
    <item name="android:listSelector">@android:color/transparent</item>
</style>

Define a new theme in themes.xml:

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:dropDownListViewStyle">@style/MyListView</item>
</style>

Apply the theme to my activity in AndroidManifest.xml:

<activity android:name=".MyActivity"
...
android:theme="@style/MyTheme">
like image 104
gpo Avatar answered Oct 13 '22 00:10

gpo


Problem: When I press the items in the spinner's list, I can see a blue background in the corners, where the 9-patch is transparent.

I think the blue background comes from android:selectableItemBackground attribute. To change this attribute, add the following to your application's theme in styles.xml:

<item name="android:selectableItemBackground">@drawable/whicheverDrawable</item>

For reference: By default, selectableItemBackground points to the following drawable in API 19 (for Theme.Holo). You should define whicheverDrawable in a similar way:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_dark" />
    <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_dark" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
    <item android:state_focused="true" android:drawable="@drawable/list_focused_holo" />
    <item android:drawable="@color/transparent" />
</selector>

In your case, you can define rounded corners for another drawable, which is used for statePressed in whicheverDrawable.

like image 44
Vikram Avatar answered Oct 13 '22 01:10

Vikram