I am using this code below, but it doesn't work properly: If I select an item, the background is changed. But the background change also if I put only focus on the item without selecting it. Why ?
Added to my listview:
android:listSelector="#drawable/bg_key"
#drawable/bg_key
<?xml version="1.0" encoding="utf-8"?>
<selector
android:id="#+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="false"
android:drawable="#color/activated_color" />
</selector>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="activated_color">#1d1d1d</color>
</resources>
You can highlight/provide ripple effect to your list items using the following :
Create a selector item_ripple.xml in your drawable :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#color/activated_color"></solid>
</shape>
</item>
<item >
<shape>
<solid android:color="#android:color/transparent"></solid>
</shape>
</item>
</selector>
Create a selector item_ripple.xml in your drawable-v21
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/activated_color">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
You need to add these selector as background of your item layout :
android:background="#drawable/item_ripple"
You can use pressed state in your selector file
/drawable/list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/holo_red_light" android:state_pressed="true"/>
</selector>
then set following attribute in your listView
android:listSelector="#drawable/list_selector"
Related
I tried to create a selector for my button, using the the following XML codes...
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" android:state_enabled="true"></item>
<item android:drawable="#drawable/rectangular_grey" android:state_pressed="true"></item>
</selector>
this is the rectangular gray shape I have defined...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/grey"/>
<stroke android:color="#android:color/transparent"/>
<corners android:radius="1dp"/>
</shape>
and this is the rectangular transparent shape I have defined...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/grey" android:width="#dimen/_1sdp"/>
<corners android:radius="1dp"/>
</shape>
When I run it into my device, it shows only transparent button but it doesn't change color when i press...
The same approach works when I try to use with other color than transparent, please help...
The order of items in a selector is important. It always picks the first one that applies. So you should swap those items.
In your case, the button is always enabled, no matter what pressed state is, so it always picks the first item.
You need to use selector in right way :
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" ></item>
<item android:drawable="#drawable/rectangular_grey" android:state_pressed="true"></item>
or
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" android:state_pressed="true"></item>
<item android:drawable="#drawable/rectangular_grey"></item>
I create a drawable.xml for some purpose , and i want to use this color:#29395e.
But i can't set up the #29395e in android:drawable .
I try to use <item android:color=" #29395e"/> , it can't compile.
Is any way i can use this color #29395e in this drawable ?
Any help will be grateful.
here is my drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/corner_with_tab" android:state_selected="true" />
<item android:drawable="#android:color/holo_blue_light"/>
</selector>
Make a "colors.xml" resource file in res/values folder
<resources>
<color name="colorName">#4da6ff</color>
</resources>
now in your mydrawable file use that color like this
<item
android:state_checked="true"
android:drawable="#color/colorName" />
Instead of creating a new xml file, you can just add this and it will work as well.
<item>
<shape>
<solid android:color="#18191B" />
</shape>
</item>
So your entire code will be
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/corner_with_tab" android:state_selected="true" />
<item>
<shape>
<solid android:color="#18191B" />
</shape>
</item>
</selector>
if your drawable.xml is right then
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/corner_with_tab" android:state_selected="true" />
<item android:drawable="#android:color/holo_blue_light"/>
</selector>
Create drawable colors.xml under values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="corner_with_tab">#3F51B5</color> // Example. Add your Hex color
</resources>
1、Edit a xml file such as abc.xml in your drawable folder.
2、<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#29395e" android:state_selected="true"/>
<item android:drawable="#29395e" />
</selector>
3、In your layout.xml,use android:src="#drawable/abc"
adding color with xml is
<item android: android:color="#color/new_color" />
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/corner_with_tab" android:state_selected="true" />
<item android:color="#color/holo_blue_light"/>
After having been looking for a while I've not been able to find an answer to this...
I have a recycler view with items which when selected have a red background and white text (beforehand the background is white and text is black). To do this I am using a selector.
I have recently tried to add a ripple effect to this, but unless I long click on the item the background of the item goes straight to red without the ripple. I am assuming this is because the selector state state_selected overrides the ripple on sate_pressed?
Does anyone know if there is a way around this? Here is the selector code I use:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_red_dark" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/ripple"
android:state_pressed="true"/>
<item
android:drawable="#android:color/holo_red_dark"
android:state_selected="true"/>
<item android:drawable="#android:color/white"/>
</selector>
</item>
</ripple>
Thanks in advance!
To create a selector background that has a ripple effect and shows selected status I do the following:
Start by defining your highlight color, with some transparency:
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="selector_color">#660000ff</color>
</resources>
You probably want to have compatibility pre-lollipop. Put a typical old-school selector inside drawable folder:
drawable/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selector_color" android:state_pressed="true"/>
<item android:drawable="#color/selector_color" android:state_selected="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
And then add the following layer drawable inside drawable-v21 folder:
drawable-v21/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_selected="true"
android:drawable="#color/selector_color" />
<item android:drawable="#android:color/transparent" />
</selector>
</item>
<item>
<ripple android:color="#color/selector_color">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
</item>
</layer-list>
Now you can use #drawable/selector_background for your selector.
So I have another case in which I had to use selector as well as layer list for that
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<!-- ripple color -->
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/c_unread_notifications_item" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<!-- ripple color -->
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
</selector>
This worked, for your need what you can do it just replace the item under ripple with your item shape if you don't have any layering.
Hope this helps
It will be better if you wrap your recyclerview item view in FrameLayout and set android:background="?selectableItemBackground" of FrameLayout and the child layout of FrameLayout background="#drawable/background"
background.xml
<item android:drawable="#color/red" android:state_selected="true"/>
<item android:drawable="#color/red" android:state_focused="true"/>
<item android:drawable="#color/red" android:state_pressed="true"/>
<item android:drawable="#color/white"/>
And then child layout must has attribute android:duplicateParentState="true"
I have buttons with different draw9patch png as background. Currently the buttons are controlled by selector which look something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/focused" android:state_focused="true"/>
<item android:drawable="#drawable/default"/>
</selector>
For the Android Lollipop they have a RippleDrawable for the touch effect, which goes something like this:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
...
</item>
</ripple>
Regarding the new touch ripple effect:
1: Can I set draw9patch as background for RippleDrawable?
2: How do I accomodate the above two different xml's I want to follow Material design? Do I have to fork out a new folder/layout xml for the new RippleDrawable?
1) Yes. See the documentation for RippleDrawable for more details on how layers are composited, but basically you want:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:drawable="#drawable/yourninepatch" />
</ripple>
Or to also handle the disabled state in a clean way, you might want:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item>
<selector>
<item android:state_enabled="false">
<nine-patch
android:src="#drawable/yourninepatch"
android:alpha="?android:attr/disabledAlpha" />
</item>
<item>
<nine-patch android:src="#drawable/yourninepatch" />
</item>
</selector>
</item>
</ripple>
2) Yes, you should place your ripple XML in drawable-v21.
I have a ListView item which has a set background. This overrides the default blue highlight that appears when the item is pressed/selected. Is there a way to have both the background and the selector?
This is my attempt at merging both a background and selector...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/red"/>
</selector>
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="6dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
This is in my drawable folder, and I set it with this in my ListItem xml:
android:background="#drawable/my_background
To have a custom background and the default selector effect (another drawalbe when pressed / selected) is a little difficult, after a few tries, I made it.
You should define two selectors in separated xml file: listitem_background.xml and listitem_selector.xml.
The first one is used to the background of the list item, it will make the effect when the item is pressed and in normal state.
The second one is used to the selector of the list, it will get rid of the default selector of the list view by setting all the state to transparent.
The default selector effect is defined in the first xml file: listitem_background.xml.
First you need a xml file to define some drawable color: color_drawable.xml, in res/values directory:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The color of the normal state. -->
<drawable name="listitem_normal">#E671B8</drawable>
<!-- The two color below show when the item is pressed, you should change that to the color you want. -->
<drawable name="listitem_pressed">#e7eeab</drawable>
<drawable name="listitem_selected">#e7eeab</drawable>
</resources>
Then, listitem_background.xml in res/drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/listitem_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/listitem_selected" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/listitem_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="#drawable/listitem_normal"/>
</selector>
and, listitem_selector.xml in res/drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/android:transparent" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#color/android:transparent" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#color/android:transparent"/>
</selector>
set listitem_background to listitem:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/listitem_background" >
...
</RelativeLayout>
set listitem_selector to listview:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/listitem_selector" />
Seeing as this is getting a bit of attention again, I will post the solution I found (which I had previously mentioned in a comment):
I found android:drawSelectorOnTop="true" in the ListView solved the problem.
Just use of this in ListView to match the color combination
android:cacheColorHint="#e7eeab"