I want to change the background of the button when the button is unclickable.I have used selector for that but it is not working in the case when the button remains unclickable.
This is the selector file:
<?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/button_lightgrey" />
<item android:state_pressed="true" android:state_enabled="true" android:drawable="#drawable/button_blue"/>
<item android:state_focused="true" android:drawable="#drawable/button_darkgreen" />
<item android:drawable="#drawable/button_lightgreen" />
</selector>
This is the button where i am using this selector file:
<Button
android:id="#+id/PrevButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="5.0dip"
android:layout_weight="1.0"
android:background="#drawable/ibtn"
android:onClick="onPrevButtonClick"
android:text="Prev" />
Please help me.All other functions are working in the selector file but only the unclickable button case is not working.Thanks
instead of button.setClickable(false) do button.setEnabled(false) and use the same selector xml.
this may helps you
i think in your selector file having issue try selector file like below
pls change images as you require
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed" android:state_focused="true" />
<item android:drawable="#drawable/img_btn_repeat"
android:state_focused="false"
android:state_pressed="false" />
</selector>
Just modify your selectors with below selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
</selector>
Hope this helps you.
Edit
As per your comments you can't use selector for button which is the state as false(button.setclickable(false);) In this state you can't use your selector
Related
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.
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'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 am utilizing list selector, but its not working acoordingly. I am gettings effects after clicking list item, but there is no effect before item is clicked. Here is my code for selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/ic_launcher" />
<item android:state_pressed="true"
android:drawable="#drawable/headerplain" />
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
</selector>
and I have applied it to listView as
<ListView
android:id="#+id/listView123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
android:focusable="true"
>
</ListView>
Please help me to get out of this problem
Thanks in advance
This is a good tutorial explaining very well how selector works
I have rowback and rowback_selected in my drawable folder...
and Use android:state_focused,android:state_selected,android:state_pressed...
<item android:state_focused="true" android:drawable="#drawable/rowback" />
<item android:state_pressed="true" android:drawable="#drawable/rowback_selected" />
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#drawable/rowback_selected" />
<item android:state_window_focused="false"
android:drawable="#drawable/rowback" />