Disable button with button selector - android

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 -->

Related

How to make selector keep the pressed state till clicked again

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>

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>

Android: Relative Layout Selector Doesn't Work

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.

Android Button background color when clicked

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.

How to get selection appearance like listview for textview in android

I have a textview and some text is present inside of it. Now if i tap on textview, i want section appearance like listview. when i press on the text, it should show me selection appearance, how to do?
use selector Link
<?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>

Categories

Resources