I have a toggle button with the following drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/ic_heart_pressed"/>
<item android:state_pressed="true" android:drawable="#drawable/ic_heart"/>
<item android:state_checked="true" android:drawable="#drawable/ic_heart_pressed"/>
<item android:drawable="#drawable/ic_heart"/>
</selector>
This is working fine. However, since this is a favorite button when I present the screen second time and it contains some items that are favorite to begin with, I want to change the default from heart to heart_pressed
I tried doing it programmatically like this in getView of my ArrayAdapter, however, with this I lose the "toggle" capability of the button.
if (item.isFav())
holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart_pressed));
else
holder.hButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_heart));
Question
Is there a way to change the default of a ToggleButton programmatically based on a condition in the code?
Related
I have an image button (star) which is used to mark something as favorite. I envision that when the user clicks on the star, the star will turn yellow. When they click on an already yellow start, it will go back to normal.
Transition from one color to the other would make a call to the server. I am doing that part already.
To change the color on click I did this.
<?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:drawable="#drawable/ic_action_fav" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:drawable="#drawable/ic_action_ic_action_star" />
</selector>
However, this changes the color only for the time being when the buttons is clicked. It doesn't remain changed on the click, in other words, it doesn't toggle.
How can I toggle the color of a button on each click?
Use android:state_selected in the state list along with View.setSelected(boolean selected) in your Java code.
I make a custom toggle button in android, checked state and unchecked state are work fine with two image, now i want set disable image for disable state of toggle button. How i do that? Thanks!
This is my code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#drawable/record_off" />
<item android:state_checked="true" android:drawable="#drawable/record_on" />
</selector>
Make these changes in your selector
<item android:drawable="#drawable/record_off" android:state_checked="false" android:state_enabled="true"/>
<item android:drawable="#drawable/record_on" android:state_checked="true" android:state_enabled="true"/>
<item android:drawable="#drawable/record_disabled" android:state_enabled="false"/>
I am using this: #drawable/record_disabled merely as an example. You can have your own name for the drawable which you will have to create just like you must have done with the ON and OFF buttons. ;-)
Note that I have added an android:state_enabled="true" attribute to all the items.
The simplest thing you can do in such cases, is go to the location where you have your SDK placed. Then choose the platform you are building for. Go to the data\res\drawable folder and look at how Google does it.
Since you are customizing a Toggle button, search for this: btn_toggle_holo_dark or btn_toggle_holo_light in the drawable folder.
<item android:state_enabled="false" android:color="#android:color/bright_foreground_dark_disabled"/>
<item android:color="#android:color/bright_foreground_dark"/>
What is the difference between these two? According to the documentation the color of the first item is used when the state is NOT enabled, and the second is the default one. So, if the item is not enabled, which color is used?
If the item is not enabled the first item is used, as it matches all its state selectors. Selector items are checked from top to bottom, and the first one for which the state matches is used.
false states are intended to be used in combination with other states. For example, you have checkable item, and it can be either disabled or enabled, and you want to have different drawables for each state combination. This can be achieved the following way:
<item android:state_checked="true" android:state_enabled="true" android:drawable="#drawable/drawable1"/>
<item android:state_checked="true" android:state_enabled="false" android:drawable="#drawable/drawable2"/>
<item android:state_checked="false" android:state_enabled="true" android:drawable="#drawable/drawable3"/>
<item android:state_checked="false" android:state_enabled="false" android:drawable="#drawable/drawable4"/>
<item android:drawable="#drawable/drawable0"/>
If you don't need such combinations, there's no need to use state_xxx="false" without other states, though that's not a mistake.
When creating button drawables, I typically follow the following format to implement an "onClick" change of background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/RESOURCE_FOR_CLICKED" />
<item android:drawable="#drawable/RESOURCE_NORMAL" />
</selector>
This works fine for me when I am creating traditional buttons as I want them to return to their original state once onClick is finished, and I don't have to implement any code.
However, this does not work for RadioButtons because I actually want their background drawable to be different in the non-pressed state once they have selected.
Are there XML attributes for states involving radio buttons that I should be aware of to implement this sort of thing? If not, do I have to manage the changing of backgrounds in code?
android:state_checked will let you specify a drawable for when a radio item is selected.
I have the following UI presented when my ListView is empty:
Now, I want that when the user will press this "New Reminder" layout, it'll change to a "highlighted" state (with the blue focus background in ICS and the yellow color in GB)
The layout is clickable and the onClick method is called, but there is no indication for the user while he press his finger down.
I tried setting focusable to true, but it didn't do the trick.
What can I do to give any view the default "pressed" effect?
Thank you!
With your layout, you can set background with drawable like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:drawable/star_big_on" />
<item android:state_pressed="true" android:drawable="#android:drawable/star_big_on" />
<item android:drawable="#android:drawable/star_big_off" />
</selector>
Draw-able like below
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/left_radio_selected"/>
<item android:state_enabled="false" android:drawable="#drawable/left_radio_inactive"/>
<item android:state_enabled="true" android:state_selected="true" android:drawable="#drawable/left_radio_selected"/>
<item android:drawable="#drawable/left_radio_active"/>
and set your linerlayout.setSelected(true);
I don't know if I understand you correctly. If you want to create pressed state for this layout, you have to prepare state drawable xml (more info). Up there you can set what should your layout looks like while in pressed state.
I'm not sure but your question seems a little vague. From my understanding what you want might be suggested in this post Force a ListView item to stay "pressed" after being clicked?