0

This is what I need:

A selector to show that you pressed it. But this selector also handles the long-press color transition. I looked a lot and found this. This is basically what I need, but this only works with images, not colors. Here is my code right now:

default_selector.xml:

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

    <!-- 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="@android:color/transparent" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@android:color/transparent" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/default_selector_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/default_selector_transition" />
    <item android:state_focused="true"                                                             android:drawable="@color/selection_grey" />
    <item android:state_activated="true" android:drawable="@color/selection_grey"/>
    <item android:drawable="@android:color/transparent" />
</selector>

default_selector_transition.xml:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_pressed_default"  />
    <item android:drawable="@drawable/list_longpressed_default"  />
</transition>

The drawables in default_selector_transition.xml are now 9 patches. I want to know if it is possible to change this to colors.

What I tried:

  • Just put a color instead of a drawable in there
  • Put a color as drawable in there (<drawable name="selection_grey_drawable">#BDBDBD</drawable>)

I'm hoping you guys have a solution for me!

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76

1 Answers1

2

Putting a color instead of a drawable didn't work for you?

If not, you can still try creating a separate drawable files with shapes inside. For example like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/your_color_1" />
</shape>

and

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/your_color_2" />
</shape>

and then linking those files inside your default_selector_transition.xml:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/first_shape"  />
    <item android:drawable="@drawable/second_shape"  />
</transition>

EDIT:

Well the main problem is that your code doesn't work for me at all, even with 9-patches...

Android developer documentation says:

<item> Defines a drawable to use as part of the drawable transition. Must be a child of a <transition> element. Accepts child <bitmap> elements.

Therefore if your code works for you, you can still try to trick drawable to use bitmap child that is being created with a color? Something like this should work:

<bitmap
    android:src="@drawable/dummy_bitmap"
    android:tint="@color/your_color"
    android:tileMode="repeat"/>

Where dummy_bitmap is just some drawable that would "fill" the needed src field in this bitmap.

maybe like this:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/NOT_transparent_dummy_bitmap"
            android:tint="@color/your_color_1"
            android:tileMode="repeat"/>
    </item>
    <item>
        <bitmap
            android:src="@drawable/NOT_transparent_dummy_bitmap"
            android:tint="@color/your_color_2"
            android:tileMode="repeat"/>
    </item>
</transition>
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • I just tried this, and unfortunately this doesn't work. This could've been a good other option. – Kevin van Mierlo Jan 19 '15 at 12:49
  • Well I just did as you said, made two files in the drawable folder. Both shapes. One is gray (#BDBDBD) and the other is darker gray (#9E9E9E). I put those shapes as you said in the transition file, but it just stays one color when long pressed – Kevin van Mierlo Jan 19 '15 at 12:54
  • Good try, I really thought this one would work. But unfortunately it didn't. I used a transparent image and tint was gray again. This time there was no selection at all – Kevin van Mierlo Jan 19 '15 at 14:23
  • Damn, I run out of ideas:) Good luck then:p If you find a solution, please paste it somewhere in this thread. – Bartek Lipinski Jan 19 '15 at 15:44
  • wait... you used transparent image? `tint` will work only if its not transparent. It can be a png file 1x1 pixels since the `tileMode="repeat"` is present (any color but not transparent) – Bartek Lipinski Jan 19 '15 at 16:15
  • Just tried it, but also doesn't work. Tried a white color. I think there is no easy way to do this than using images – Kevin van Mierlo Jan 19 '15 at 17:58