Rectangular subject pressed blue background

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

Problem: when I click elements in the list of counters, I can see a blue background in the corners where the 9-patch is transparent.

I can not get rid of this blue background that appears when I click elements. If I remove the 9 patches and any settings in the spinner, I see that the items in the list by default are gray and blue when clicked.

I also tried not to use 9 patches as a background, but only a color selector, and set the pressed color in the selector to be transparent. Then, when I click on an item, it is not transparent, but blue. I think the view in the list is really transparent, but when you click on it the blue color still remains ...

I am using a custom SpinnerAdapter to create an element 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 only with color, and without a 9 patch. 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 installed a custom adapter on the counter:

    spinner.setAdapter(new MySpinnerAdapter());

And the spinner is extracted from the 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 tried to set many different attributes on Spinner and tried some style attributes, but I could not get rid of this blue background ...

+4
2

@Vikram .

, Spinner , , android:listSelector. :

styles.xml:

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

topic.xml:

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

AndroidManifest.xml:

<activity android:name=".MyActivity"
...
android:theme="@style/MyTheme">
+3

: , , 9- .

, blue background android:selectableItemBackground. , styles.xml :

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

: selectableItemBackground API 19 ( Theme.Holo). whicheverDrawable :

<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>

drawable, statePressed whicheverDrawable.

+4

Source: https://habr.com/ru/post/1531138/


All Articles