I want to create an animation like material ripple animation, so I decided to look at its source code.
The enter point is :
android:background="?android:attr/selectableItemBackground">
go to: Android\Sdk\platforms\android-30\data\res\values\attrs.xml
<!-- Background drawable for bordered standalone items that need focus/pressed states. -->
<attr name="selectableItemBackground" format="reference" />
Next we go into Android\Sdk\platforms\android-30\data\res\values\themes.xml
<item name="selectableItemBackground">#drawable/item_background</item>
Nothing special, just another drawable.
Next in: Android\Sdk\platforms\android-30\data\res\drawable\item_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#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="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focused" />
<item android:drawable="#color/transparent" />
</selector
We are close, in this file we saw drawables of different states. Transition file at:
Android\Sdk\platforms\android-30\data\res\drawable\list_selector_background_transition.xml is:
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/list_selector_background_pressed" />
<item android:drawable="#android:drawable/list_selector_background_longpress" />
</transition>
Finaly we have 4 files, in picture below
So the questions is:
How this 4 squares turns into this circular ripple animation?
That means this black pointers?
what the orange fill and green border do ?
(optional) Where is the magic part?
Related
Can anyone help me with changing the background color of my button so that it can change color when clicked and change back to normal color after clicked. I am not interested in using images and have looked, everyone keep posting half the codes so I still don't understand fully. If possible, please provide a link where I can see step by step instructions, These half posted comments does not help me at all because I am totally new to android and Java.
You need to make special drawable for that with selector element.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and in the button
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
More info here
<item
android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<item
android:state_pressed="true"
android:drawable="#drawable/item selected"/>
<item
android:state_focused="true"
android:drawable="#drawable/item selected"/>
<item
android:drawable="#android:color/transparent"/>
Put this xml in a drawable file and set the resource.
This is what I use for list item selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item android:drawable="#color/red" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#color/green" android:state_pressed="false"/>
<!-- normal -->
</selector>
I have enabled a context menu for the ListView items so a users can long click on an item. What I want is that when a user long clicks an item, the colour should change from green to red. How can I achieve that?
You can use list_selector_background as it, As suggested here
<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="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="#+drawable/list_selector_background_focus" />
</selector>
And use a transition for long press, As suggested here:-
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector_background_pressed" />
<item android:drawable="#drawable/list_selector_background_longpress" />
</transition>
May be it will help you..
Referring to Android: How to achieve the glow effect when long-pressing a list item?
By applying list_selector_holo_light.xml on a linear layout's background (Which is NOT being used in ListView) , I thought I can achieve glowing animation effect by long press on it.
<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="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
However, I do not see the transition animation effect, to transits from light blue to dark blue.
Is there anything else I had missed out?
I have set a custom list selector for a Simple ListView but when I select an item, the whole ListView turns blue. I don't understand where the problem is.
This is my ListView:
<ListView android:id="#+id/TopListView" android:layout_width="fill_parent"
android:listSelector="#drawable/regular_selector"
android:layout_height="wrap_content">
</ListView>
and it's the regular_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/blue" />
<item android:state_focused="true"
android:drawable="#color/blue" />
</selector>
I was having the same issue of the whole list view highlighting, when using a color as the background. Curiously this only happened below api 11.
Solution was to use a solid shape drawable to wrap the colour:
list_selector_shaped_background_press.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/list_selector_pressed"/>
</shape>
List_selector_background.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="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
I guess it is the usual problem. You have to set this in your listview:
android:cacheColorHint="#00000000"
The goal is to disable an optimization that the framework does for improving drawing performance during scrolling operations.
When I set the regular_selector.xml as listview item background it work!
For the default android button, when I click on it, its color is changed to orange for a few seconds (for notifying the user the button is clicked). Anyway, I don't want this property. I don't want to change my button color. How can I disable this property?
Have a look on how button is styled by Android:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
</selector>
To style your button with Android's themes images resources, just create your own selector by copying the Android's one and replace all #drawable/btn_default_* by #android:drawable/btn_default_normal in items tag (and just remove items you don't want). For example :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="#android:drawable/btn_default_normal" />
</selector>
Then, apply this drawable selector as a android:background of your button.
Hope it helps.
(If you want, I can also provide an example of applying this background easily to all your buttons, just ask.)