I have this selector for one of my button's background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/bg_botton_disable"/>
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/bg_botton_pressed"/> <!-- pressed -->
<item android:state_enabled="true"
android:drawable="#drawable/bg_botton" /> <!-- default -->
</selector>
(Button) bt = (Button) findviewById(R.id.MyBotton);
bt.setEnabled(false);
The code disables the functionality of my button. I mean when I press the button it doesn't launch its own onClick() method. That's fine, but the problem is that it doesn't change the background of the button to bg_botton_disable drawable.
Does anyone has any suggestion how to fix it?
Reorder your selector items
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="#drawable/bg_botton" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="#drawable/bg_botton_pressed"/> <!-- pressed -->
<item android:state_enabled="false"
android:drawable="#drawable/bg_botton_disable"/>
</selector>
Related
I use selector to create custom buttons with
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
Now I want that the button acts as a state button remaining in the pressed status till is clicked again to return unpressed.
If possible I want to avoid to achieve this setting status programmatically.
Is there a way to do this using selectors (or something similar)?
Can you use ToggleButton instead of Button?
layout.xml
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:textOff="done"
android:textOn="done"
android:background="#drawable/test_selector"/>
test_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- on checked -->
<item
android:state_checked="true"
android:state_pressed="false"
android:drawable="#drawable/button_bg_selected" />
<!-- normal -->
<item
android:state_checked="false"
android:state_pressed="false"
android:drawable="#drawable/button_bg_normal" />
<!-- pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
</selector>
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>
I want to change background of a button when it is clicked. I tried to use a selector. But It didn't work. Here is the selector (add_grp_slctr.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp_d" />
<item android:drawable="#drawable/add_grp" />
</selector>
And the button :
<Button
android:id="#+id/addGrpBtn"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="#drawable/add_grp_slctr"
android:onClick="addGrpDialogOpen" />
add_grp_d and add_grp are images(png).
I tried a similar code which will be white by default, black when pressed on an onclick of a button:
//***This is the btn_selector which is to be declared in drawable folder***
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:color/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
and called this on the button.xml -->
android:background="#drawable/btn_selector"
Hope this would help..:)
go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. Also state_focussed for button only works when you are focussing the button using a hardware-keyboard.
As for your case
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_selected="false" android:drawable="#drawable/button_default"/>
<item android:state_pressed="true" android:state_selected="false" android:drawable="#drawable/button_default" />
<item android:state_pressed="false" android:state_selected="true" android:drawable="#drawable/button_selected"/>
<item android:state_pressed="true" android:state_selected="true" android:drawable="#drawable/button_selected" />
<item android:drawable="#drawable/button_selected" />
</selector>
Use your selector
change your code like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp" />
<item android:drawable="#drawable/add_grp_d" />
</selector>
My Code
<item android:drawable="#drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="#drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="#drawable/shadow_design"/>
</selector>
I think you should change your selector a bit.
Check this answer here.
Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way.
Button lineColorCode = (Button)findViewById(R.id.button1);
Now inside button's click event use following code.
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);
I am creating a spinner with a custom view, anyway I managed to show different drawables for when the spinner is inactive and also for when it's pressed, I would like to keep the pressed state drawable when the dropdown list shows. Here is mi XML file:
<?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="false"
android:drawable="#drawable/ComboBoxInactive" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/ComboBoxActive" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/ComboBoxActive" />
<item android:drawable="#drawable/ComboBoxInactive" />
</selector>
what state should I add for when it is displaying the dropdown? I want it to display ComboBoxActive drawable. I already tried adding this:
<item android:state_enabled ="false"
android:drawable="#drawable/ComboBoxActive"/>
Any idea of what the state is?
There is no android:state_dropdown_showing state.
The only one state on spinner dropdown list is state_enabled="true"
You can use my selector to differentiate dropdown list state
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:drawable="#drawable/spinner_off"/>
<!-- pressed state -->
<item android:state_enabled="true" android:state_window_focused="true" android:state_pressed="true" android:drawable="#drawable/spinner_pressed"/>
<!-- unselected state -->
<item android:state_enabled="true" android:state_window_focused="true" android:drawable="#drawable/spinner_default"/>
<!-- dropdown list state -->
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/spinner_dropdown_list_is_shown"/>
<!-- default -->
<item android:drawable="#drawable/spinner_default"/>
</selector>
Don't forget to set setFocusable and setFocusableInTouchMode properties on spinner.
Based on Olef Koshkin answer I can add that if you want save changed state after click to spinner and return it to default only after close spinner you can use. It works for me.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="#drawable/custom_spinner_inactive"
android:state_enabled="false"/>
<!-- pressed state -->
<item android:drawable="#drawable/custom_spinner_inactive"
android:state_enabled="true"
android:state_pressed="true"
android:state_window_focused="true"/>
<!-- unselected state -->
<item android:drawable="#drawable/custom_spinner_inactive"
android:state_enabled="true"
android:state_window_focused="true"/>
<!-- dropdown list state -->
<item android:drawable="#drawable/custom_spinner_inactive"
android:state_enabled="true"
android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/custom_spinner_active"/>
</selector>
In other cases I see blink of default state and I didn't like it.
This works for me
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- For DropDown is Not Visible -->
<item android:drawable="#drawable/spinner_bg_with_arrow_border_theme_color" android:state_window_focused="false" />
<!-- For DropDown is enter code here Visible -->
<item android:drawable="#drawable/spinner_bg_with_arrow_border" android:state_window_focused="true" />
</selector>
I want to set the color of row in listview if selected to yellow and otherwise be white so I use the following selector
<?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="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="#color/encounterselector_color" />
<item
android:drawable="#color/encounter_normal" />
</selector>
where
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="encounterselector_color">#fbeda5</color>
<color name="encounter_normal">#ffffff</color>
</resources>
and I use it like the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/EncounterGrid"
android:background="#drawable/encounterlistview"
>
<!-- remaining code -->
but the row is always white , any idea how to fix that
Setting the background color with a selector is a bit tricky. Basically you have to create a drawable for each color and use them in your android:drawable attributes. You cannot directly use colors.
Check this related question for more details.
I use the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the switched off state -->
<item android:state_enabled="false"
android:drawable="#drawable/grey_bar" />
<!-- These are the partial states -->
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<!-- This is the switched on state -->
<item android:state_enabled="true"
android:drawable="#drawable/button_normal" />
</selector>
Where all of the drawables I point to are defined in xml, or are existing 9 patch images.
use android:color on your selector and not android:drawable because you are getting reference to the color resources, so your selector will be like this:
<?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:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:color="#color/encounterselector_color" />
<item
android:color="#color/encounter_normal" />
</selector>
Nothing was working for me until I set drawSelectorOnTop = "true".
Everything worked after that.