Set image of disable state for custom toggle in android - android

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.

Related

Android Switch set thumbResource programmatically

I need to add customised Switch to my application. I know that usually to display it in a way you like you need to create a selector for thumb and track resources and set it in the XML, which I did and it works. Code for selectors is as follows:
1. thumb_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_thumb_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_thumb_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/switch_thumb_activated" android:state_checked="true"/>
<item android:drawable="#drawable/switch_thumb"/>
</selector>
bg_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switch_bg_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/switch_bg_focused" android:state_focused="true"/>
<item android:drawable="#drawable/switch_bg_on" android:state_checked="true"/>
<item android:drawable="#drawable/switch_bg"/>
</selector>
These selectors are located in drawable resource folder, so when I use
android:thumb="#drawable/thumb_selector"
android:track="#drawable/bg_selector"
switch is displayed as expected. However, there are other properties and many Views I have to create dynamically, so it is better to add a custom control (which extends Switch), and I need to set ThumbResource and TrackResource. Documentation says there are two methods, which can set it: setThumbResource(int resId) and setTrackResource(int resId), but when I try to reference selector as setThumbResource(R.drawable.thumb_selector) it doesn't recognise this selector as drawable. It's a silly problem and I can't seem to be able to solve it, did I add it to the wrong folder? One way to work around this is to extend layout and inflate xml version of Switch, but do I have to do this? How can I reference selector within the code?

How to programmatically change default for the toggle button

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?

Setting button drawable for RadioButton using drawable xml not working for checked state

I've got a group of radio buttons, and I want to set the button's background to a solid color when checked. I created a drawable resource, using a selector and item def's like:
<item android:state_checked="true" android:state_pressed="false"
android:drawable="#color/app_tint"/>
with several variations while trying to get it to work. In the layout containing the buttons, I've tried setting both button and background properties (not at the same time, just one or the other in testing) like:
android1:background="#drawable/radio_state"
OR
android1:button="#drawable/radio_state"
I've read several posts, and I feel I'm close, just missing something to get it done. Thanks.
Here's one we did for an app:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_checked="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_selected="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling_focus"
android:state_focused="true" />
<item android:drawable="#drawable/ic_bcnav_ebilling" />
</selector>
Each state has a different drawable, although in this example, we don't really care about all states being very different - just focus=true get a highlighted drawable (it has "..._focus")

How to enable the "pressed" look on LinearLayout

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?

Button Background Selector

I try to switch the background of Buttons if they are pressed. I build a Selector like the answer suggested here: Standard Android Button with a different color
Finally I want to put GradientDrawables inside, but for debugging purposes I just set a color, to check if it works.
Here is my Selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/red"/>
<item
android:state_focused="true"
android:drawable="#color/white"/>
<item
android:state_pressed="true"
android:drawable="#color/white"/>
</selector>
Unfortunatly this doesn't work. I set the Selector as Button background and only see them in red color. What Am I doing wrong (Build Target 2.1)
put this at the end
item android:drawable="#color/red"
i mean as the third option, it will work.
android checks the xml conditions from the start, the first tag doesn't have any condition, so it will always pick red, so you have put conditions first and then the default one.
here is the code I use, and it works really well.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_clicked"
android:state_pressed="true" android:state_enabled="true" />
<item android:drawable="#drawable/button" android:state_enabled="true" />
</selector>
here I use two images I made using photoshop as a background
the first is button_clicked and the second is button
copy it and change use your own resources.
hope I could help :)

Categories

Resources