I'm using a relative layout with a selector resource xml to highlight the relative layout on click. But it doesn't highlight the relative layout on click. Below i have put the corresponding selector xml, layout xml and the drawable.
Layout Code Snippet:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:id="#+id/rlShare"
android:background="#drawable/icon_selector"
android:clickable="true">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:contentDescription="#string/cd_new_exam"
android:scaleType="fitXY"
android:src="#drawable/share_icon" />
</RelativeLayout>
Selector xml file:
<!-- Selected -->
<item android:drawable="#drawable/icon_selected" android:state_focused="true" android:state_selected="false"/>
<!-- Pressed -->
<item android:drawable="#drawable/icon_selected" android:state_focused="false" android:state_selected="true"/>
Please help me with this,
Thank you in advance.
use this code and take different image from drawable
<!-- Selected -->
<item android:drawable="#drawable/icon_selected" android:state_focused="true" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="#drawable/icon_pressed" android:state_focused="false" android:state_selected="true"/>
<!-- When not selected -->
<item android:drawable="#drawable/icon_default"/>
Use this for your selector:
<!-- Selected -->
<item android:drawable="#drawable/icon_selected" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="#drawable/icon_pressed" android:state_pressed="true"/>
<!-- Default -->
<item android:drawable="#drawable/icon_default" />
Notice, that in your original selector, you have the same drawables in both states.
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 added the following selector to my listview in my drawerlayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item android:state_focused="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- pressed -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
</selector>
colorPrimaryDark and colorPrimary are just color values
This work amost fine. But if i press and release an listitem on API 19 or lower, the item gets blue. How is that possible?
If you are using AppCompat as a theme you should reference the colors from theme like this
<item android:drawable="?attr/colorPrimary"
I found the problem.
android:choiceMode="singleChoice"
This appeared to be the problem.
android:cacheColorHint="#00000000"
add this attribute for your listview
Can anyone help me with changing the background color of my button so that it can change color when clicked and change back to normal color after clicked. I am not interested in using images and have looked, everyone keep posting half the codes so I still don't understand fully. If possible, please provide a link where I can see step by step instructions, These half posted comments does not help me at all because I am totally new to android and Java.
You need to make special drawable for that with selector element.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and in the button
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
More info here
<item
android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<item
android:state_pressed="true"
android:drawable="#drawable/item selected"/>
<item
android:state_focused="true"
android:drawable="#drawable/item selected"/>
<item
android:drawable="#android:color/transparent"/>
Put this xml in a drawable file and set the resource.
I have an ImageButton and I want to make it so the button background changes color when the button is pressed. I have copied the button_bg.xml file from this question.
button_bg.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
And line #54 looks like this:
<ImageButton
android:id="#+id/sendButton"
android:background="#drawable/button_bg"
android:src="#drawable/ic_action_send_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_alignParentRight="true" />
I have tried removing the line:
android:background="#drawable/button_bg"
which stops the application crashing but the buttons don't change color.
Any help would be appreciated
By changning button_bg.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/clr_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#android:color/transparent"/> <!-- focused -->
<item android:drawable="#android:color/transparent"/> <!-- default -->
</selector>
and adding the following to strings.xml
<drawable name="clr_normal">#AAAAAA</drawable>
<drawable name="clr_pressed">#777777</drawable>
the problem was solved and the code worked as intended.
I have a button selector that changes the button image when it is pressed. I have also set an image for when the button is disabled. I try and disable the button programmatically but the disabled button image is not appearing. Is my button_selector correct?
<item android:drawable="#drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_focused="true"/> <!-- focused -->
<item android:drawable="#drawable/red_btn_bg"/> <!-- default -->
I am using mButton.setEnabled(false) in my code to disable the button
try this one and i uploaded one sample project for you for more help check the project
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_disable" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal"/>
</selector>
and use this selector in button as following
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="30dp"
android:background="#drawable/selector"
android:enabled="false"
android:text="Disable Button" />
sample code link
https://www.dropbox.com/s/lydkog10rkujbsa/ButtonSelector.rar
Try this.
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="#drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="#drawable/red_btn_bg"/> <!-- default -->