Android Button selector not working properly - android

I am using the following selector to change the color of a button
<?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/darkgray" />
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/background_red_down" />
<item android:drawable="#color/background_red" />
</selector>
I am getting the red color for the button but that's it. I am not getting the gray disabled color, the button is indeed disabled as it is unclickable. I am also not getting the red_down color once I click.
What have I missed?
Thanks

in your color.xml
replace this
<color name="background_red">#9A9484</color>
to
<drawable name="background_red">#9A9484</drawable>
"#9A9484 = your background color"

This is code from my button selector. Modify it's values and it should work for you.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/redtheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/redtheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="#drawable/redtheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/redtheme_btn_default_focused_holo_light" />
<item android:state_enabled="true"
android:drawable="#drawable/redtheme_btn_default_normal_holo_light" />
<item android:state_focused="true"
android:drawable="#drawable/redtheme_btn_default_disabled_focused_holo_light" />
<item
android:drawable="#drawable/redtheme_btn_default_disabled_holo_light" />
</selector>

Related

Change all Color on Text Controller

I need change all color for EditText, I tried all my information
ex:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#color/colorJust" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#color/colorJust" />
<item
android:state_pressed="true"
android:drawable="#color/colorJust" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#color/colorJust" />
<item
android:state_enabled="true"
android:drawable="#color/colorJust" />
<item
android:state_focused="true"
android:drawable="#color/colorJust" />
<item
android:drawable="#color/colorJust" />
</selector>
with: android:background="#drawable/edittext_bg"
But result is not good, the pink color is always on top when click and select text
I need to remove this PINK color i hate it!
The colour of the checkboxes, cursor, etc. is determined by your accent see here
Try adding this to your styles:
<item name="android:colorAccent">#color/colorJust</item>
you can choose android:textCursorDrawable="#null", the cursor will use android:textColor color then. or to remove cursor from edittext you can do something like android:cursorVisible="false"

Remain Highlighted in Selector.xml

I have the following selector.xml implementation, however, it only highlights when user clicks, however I want to be highlighted until other button clicks. How could I achieve it?
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/redo_off" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/redo_on" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/redo_on" />
<item
android:state_enabled="true"
android:drawable="#drawable/redo_off" />
</selector>
Try defining an item for
android:state_selected="true"
as first item of your selector.
Edit :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/redo_on" />
<item
android:state_enabled="false"
android:drawable="#drawable/redo_off" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/redo_on" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/redo_on" />
<item
android:state_enabled="true"
android:drawable="#drawable/redo_off" />
</selector>
I haven't tried it myself. If it doesn't work take a look at this similar post.

Android: Toggle Button OnPress state

I have a ToggleButton which has a selector backgroundDrawable. The ON and OFF state work perfectly fine, i.e. they change colour as I want them to. However, I am trying to colour the Togglebutton when it is pressed, i.e. touched AND not released.
selector:
<item android:drawable="#drawable/category_unselected_background"
android:state_checked="false"/>
<item android:drawable="#drawable/category_highlighted_background"
android:state_pressed="true"/>
<item android:drawable="#drawable/category_unselected_background"/>
What am I doing wrong?
that's the code I use for any button selector I'm using, always works like a charm, suit yourself :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
<item android:drawable="#drawable/plus_icon_pressed" android:state_focused="true" android:state_pressed="false" />
<!-- Button Focused Pressed-->
<item android:drawable="#drawable/plus_icon_pressed" android:state_focused="true" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="#drawable/plus_icon_pressed" android:state_focused="false" android:state_pressed="true" />
<!-- Button Default Image-->
<item android:drawable="#drawable/plus_icon" />
</selector>
The following worked as it accounts for all states. Since I was working with a ToggleButton, I needed the checked state too:
<!-- Button Focused-->
<item android:drawable="#drawable/category_highlighted_background" android:state_focused="true" android:state_pressed="false" />
<!-- Button Focused Pressed-->
<item android:drawable="#drawable/category_highlighted_background" android:state_focused="true" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="#drawable/category_highlighted_background" android:state_focused="false" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="#drawable/category_selected_background" android:state_checked="true" />
<!-- Button Default Image-->
<item android:drawable="#drawable/category_unselected_background" />
here is an example :), too on here
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Touch-->
<item android:drawable="#drawable/icon_background_pressed" android:state_pressed="true" />
<!-- Button ON-->
<item android:drawable="#drawable/icon_background_selected" android:state_checked="true" />
<!-- Button Default Image-->
<item android:drawable="#drawable/icon_background_normal" />
</selector>

How to change only color of a pressed textView?

I'm using this code sheet to define textView's color in different events. The textView is in listView (10+ items) and the effect (color) applies on every textView in every listItem. How I can change color of only textView that is pressed?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:color="#color/text_white" />
<item
android:state_focused="false"
android:state_pressed="true"
android:color="#color/text_white" />
<item
android:state_focused="true"
android:color="#color/text_white" />
<item
android:state_selected="true"
android:color="#color/text_white" />
<item
android:state_checked="true"
android:color="#color/text_white" />
<item
android:state_selected="false"
android:state_checked="false"
android:state_focused="false"
android:state_pressed="false"
android:color="#color/text_blue" />
</selector>
try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" //<<<<<Here focus us true then txt_pressed
android:state_pressed="false"
android:drawable="#drawable/txt_pressed" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/txt_pressed" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/txt_pressed" />
<item android:drawable="#drawable/txt_default" />
</selector>
In the xml file, to the textview you want to change color, add
android:background="#drawable/filename"

Remove highlight effect from EditText

I want to remove the orange highlight around the EditText. This highlight doesn't match my UI and I really want to get rid of it.
Any ideas?
EditText uses selector for it's background.
From android source code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_pressed" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_selected" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled_selected" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
You have to create you own selector where you can use your own drawables for pressed and selected state. You can read more about selectors here

Categories

Resources