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"/>
Related
If boxBackgroundColor is transparent then boxStroke is invisible
<style name="OutlinedRoundedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxBackgroundColor">#android:color/transparent</item>
<item name="boxStrokeColor">#android:color/white</item>
<item name="boxStrokeWidth">4dp</item>
</style>
So is it only possible to have transparent TextInputLayout without outline using custom background?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#color/colorEditTextOutline" />
</shape>
</item>
</layer-list>
If you had posted a design for it then it would have been easier understand. For this solution If I understand you correctly, you need to add these two things
In style add:
<style name="InputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_box</item>
</style>
Add color resource:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red_bright" android:state_focused="true" />
<item android:color="#color/red_bright" android:state_hovered="true" />
<item android:color="#color/red_bright" />
</selector>
This solution will have something like this if this is what you want
When you create an android button, the original color is grey. when you press it, the color changes, when you release it, it goes back to it's original grey.
that's what I would like to do with mine, but with other colors than the default grey. The problem is, when I change the color of my button then I click on it, nothing happens, as if I was clicking on an empty layout.
Here's the XML file i wrote to solve my problem - button_connexion_style.xml
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:color="#449D44"/> <!-- default -->
<item android:drawable="#278727" android:state_pressed="true"/>
</selector>
In my button XML code I do this to call the previous XML :
android:background="#drawable/button_connexion_style"
But it doesn't work.
If anyone can help, please let me know.
flat_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/rect_pressed"/>
<item android:drawable="#drawable/rect_normal"/>
</selector>
rect_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rect_pressed" />
<item android:bottom="#dimen/layer_padding">
<shape android:shape="rectangle">
<corners android:radius="#dimen/corner_radius" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
</layer-list>
rect_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="#dimen/corner_radius" />
<solid android:color="#color/colorPrimaryDark" />
</shape>
add below line in your button
android:background="#drawable/flat_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/red" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/blue" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/green" />
<item
android:state_enabled="true"
android:drawable="#color/grey" />
</selector>
Create an xml file in your drawable like above,
And set images/color accordingly and then set this xml as background of your imageButton.
Please do fill with according to your color code,
button_connexion_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="#color/blue" android:state_focused="true" /> <!-- focused -->
<item android:drawable="#color/default_green" /> <!-- default -->
</selector>
and add these color code in values colors ,
<color name="blue">#ff0000ff</color>
<color name="green">#278727</color>
<color name="default_green">#449D44</color>
You are almost right just one thing you miss default should always last statement
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:drawable="#278727" android:state_pressed="true"/>
<item android:drawable="#color/yourDefaultColor" />
</selector>
let me know if not work
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"
Here's the layout of a button selector I'm trying to build:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/btt_down" />
<item android:state_enabled="false" android:drawable="#drawable/btt_disabled" />
<item android:drawable="#drawable/btt_normal"/>
</selector>
And here's the layout of the normal state button (btt_normal.xml).
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#color/btt_normal"/>
<corners
android:radius="#dimen/rounded_rect_corner_radius"
/>
<padding
android:left="#dimen/rounded_rect_padding"
android:top="#dimen/rounded_rect_padding"
android:right="#dimen/rounded_rect_padding"
android:bottom="#dimen/rounded_rect_padding"
/>
</shape>
The only diff between this layout and the layout of btt_down.xml and btt_disabled.xml is this line:
<solid android:color="#color/..."/>
I would like to know if there's a way to defined a neutral (color free) rounded rectangle drawable resource, and to somehow (inheritance?) assign different colors to it and use them in the selector?
I understand that I can cut down on resources by defining all the rounded rects as part of the selector, but for maintenance sake, I would very much like to avoid copy/pasting the same code lines over and over again with just one color line differentiating between them.
Thank you.
You can define an xml like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
in your res/color folder and use it as color in your drawable layout.
To answer my own question, I couldn't find a way to avoid redefining the rectangle.
So instead of having a file for each state (normal, pressed, focused, disabled) plus a selector file, I ended up putting all the states in a single selector file with just the color differentiating between the states. The foreground color is also a selector, and the colors and rectangle dimensions are retrieved from value resource files.
Here's the directory outline of all participating files:
project
+-color
+ btt_fg_selector.xml
+-drawable
+ btt_bg_selector.xml
+-res
+-colors.xml
+-dimens.xml
Following are the selector files with pressed and normal states, and for the sake of a complete answer - value resource files as well:
btt_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true">
<shape>
<solid android:color="#color/btt_bg_pressed" />
<corners android:radius="#dimen/btt_rr_radius" />
<padding android:left="#dimen/btt_rr_padding" android:top="#dimen/btt_rr_padding" android:right="#dimen/btt_rr_padding" android:bottom="#dimen/btt_rr_padding" />
</shape>
</item>
<!-- normal/default -->
<item>
<shape>
<solid android:color="#color/btt_bg_normal" />
<corners android:radius="#dimen/btt_rr_radius" />
<padding android:left="#dimen/btt_rr_padding" android:top="#dimen/btt_rr_padding" android:right="#dimen/btt_rr_padding" android:bottom="#dimen/btt_rr_padding" />
</shape>
</item>
</selector>
btt_fg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/btt_fg_pressed" /> <!-- pressed -->
<item android:color="#color/btt_fg_normal" /> <!-- normal/default -->
</selector>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="btt_bg_normal">#0000ff</color>
<color name="btt_bg_pressed">#ffff00</color>
<color name="btt_fg_normal">#ffff00</color>
<color name="btt_fg_pressed">#ff0000</color>
</resources>
dimens.xml
<resources>
<!-- Round rect values -->
<dimen name="btt_rr_radius">15dp</dimen>
<dimen name="btt_rr_padding">10dp</dimen>
</resources>
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"