Android: Toggle Button OnPress state - android

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>

Related

How to add press effect or selector indicator to astuetz/PagerSlidingTabStrip?

I used astuetz library and implemented PagerSlidingTabStrip for my android application, it's working find. Now I want to change pressing effect, I tried:
android:background="#drawable/tab_selecor"
Code for tab selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/darkGreen" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="#color/darkGreen" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="#color/green" />
</selector>
The press colour suppose to be changed to dark green but as you see it does not change, it still looks like this even if I use another colour like red, yellow, .... .
Make use of setIndicatorColor() for setting the color of indicator and setIndicatorHeight() for setting the height
And make use of setTabBackground() method to set the background to tab
Use this drawable for tab background
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#color/red" /> <!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#android:color/black" />
<!-- Pressed tab -->
<item
android:state_pressed="true"
android:drawable="#android:color/transparent" />
<!-- Selected tab (using d-pad) -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#android:color/transparent" />
</selector>

List item long press transition

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

State list drawable and disabled state

I have a button and I want it to have different background when I set:
android:enabled="false"
Here's resource file for background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/bttn_orange_normal" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/bttn_orange_selected" /> <!-- focused -->
<item android:state_enabled="false" android:drawable="#drawable/bttn_grey_disabled"/>
<item android:state_enabled="true" android:drawable="#drawable/bttn_orange_normal"/>
</selector>
But button still has normal background when it is disabled.
What am I missing?
Put this line :
<item android:state_enabled="false" android:drawable="#drawable/bttn_grey_disabled"/>
as first item (it must be first item, otherwise it will not work) of the selector tag.
Final :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/bttn_grey_disabled"/>
<item android:state_pressed="false"
android:drawable="#drawable/bttn_orange_normal" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/bttn_orange_selected" /> <!-- focused -->
<item android:state_enabled="true" android:drawable="#drawable/bttn_orange_normal"/> <!-- idle state -->
</selector>
Vincent Ducastel's answer is correct, however it does not describe why the solution works.
When Android traverses the list of available items, it traverses the list from top to bottom, in each case evaluating whether the current state of the view matches the states defined for each item. It then selects the first item that matches the conditions and ignores the rest.
This is why you should always provide a default item at the bottom of the list and also provides a means of displaying complex selection conditions. For example if you wanted to have a special pressed state when the item is disabled, you would define the following items:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
...
<item android:state_enabled="false" android:state_pressed="false" android:drawable="#drawable/btn_grey_disabled"/>
<item android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/btn_white_disabled_selected"/>
...
</selector>
Added example of Active, Default and Disable State
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--Disable-->
<item android:drawable="#drawable/ic_button_disable"
android:state_enabled="false"
android:state_pressed="false" />
<item android:drawable="#drawable/ic_button_disable_touch"
android:state_enabled="false"
android:state_pressed="true" />
<!--Default-->
<item android:drawable="#drawable/ic_button_default"
android:state_pressed="false"
android:state_selected="false" />
<item android:drawable="#drawable/ic_button_default_touch"
android:state_pressed="true"
android:state_selected="false" />
<!--Active-->
<item android:drawable="#drawable/ic_button_active"
android:state_enabled="true"
android:state_selected="true"
android:state_pressed="false" />
<item android:drawable="#drawable/ic_button_active_touch"
android:state_enabled="true"
android:state_pressed="true"
android:state_selected="true" />
</selector>

selector for button not working

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

How to change only color of a pressed textView?

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"

Categories

Resources