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.
Related
I have a list where the list items have 2 clickable icon inside. The problem is that on Android 2.3 to Android 4.0.3 when I press a list item not only the row gets highlighted but also the icons are highlighted, but on Android > 4.1 this is not the case, here it works like it should, only the row gets highlighted.
The selector of my list:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item android:state_window_focused="false" android:state_activated="false" android:drawable="#android:color/transparent" />
<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/dna_list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/dna_list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
<item android:state_activated="true" android:drawable="#drawable/list_longpressed_holo" />
And this is the selector of the icons:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto">
<item android:state_window_focused="false" android:state_activated="false" android:drawable="#android:color/transparent" />
<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/dna_list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/dna_list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
I already searched a lot and tried to find out the problem but I am not getting it. Was the beheviour changed on Android 4.1?
The problem was that till exclusive android 4.1 the pressed state is also given to the children of the root list view and seeing that I defined following:
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/dna_list_selector_background_transition_holo_light" />
the icons in the list item is till android 4.1 pressed but not focused. I don't know if this is a bug or designed so, but it doesn't make sanse for me that the children also get the pressed state.
Starting with Android 4.1 the icons in the list item doesn't get the pressed state anymore.
Solution was to change the above selector to:
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#android:color/transparent" />
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>
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..
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"
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