<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.
Related
If I have a color list called color_list_1 like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF0000"/>
<item android:color="#00FF00"/>
</selector>
I would like to be able to create another color list called color_list_2:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000FF"/>
<item android:color="#color/color_list_1"/>
</selector>
What this allows me to do is have a control, Foo, which uses color_list_1 for a color. Now I may want to create a subclass called AmazingFoo which I want to keep the same look as Foo except I also want to add a pressed color to it.
I have tried this but it seems when using color_list_2 as the color, the states are ignored inside color_list_1. This means the pressed state works (because it is in color_list_2 directly) but when it falls back to color_list_1 the focused state is always false so the default color is returned.
I know it isn't a problem with the control because using color_list_1 works perfectly. It is only when I cascade it inside another colorlist that all of the states return false.
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?
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.
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?
I'm setting a selector.xml to the ListView Selector :
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="true" android:drawable="#drawable/timeline_selected_rect"/>
but setting this is causing the TextView flickr over selection. this thing is not happening when the Default selector is used... whats wrong with this selector.
I have even added android:cacheColorHint="#00000000" as provided info by some other blogs.
I recently ran into this problem as well and found the solution by analyzing the Android source code.
You need to remove the android:state_selected="true" attributes since checking that state is unneeded. Once a list item loses it's focus, it also makes 'selected = false'. Since focus=false & selected=true will never occur, your first 2 <items> in the selector will never be shown.
Think of the tap like a mouse click with an onPressDown and onPressUp event.
So your new selector.xml should looks something like this to avoid the flickering:
<!-- {comment copied directly from Android source code}
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_pressed="true" android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect"/>
<item android:state_pressed="true" android:state_focused="false"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_focused="true"
android:drawable="#drawable/timeline_selected_rect" />
<item android:state_window_focused="false"
android:drawable="#android:color/transparent" />
As an aside, you should seriously consider creating a separate drawable for the pressed state so that you give a bit of visual feedback. It is always nicer to see "something happen" when interacting with the UI; whether that is drawing a button in a "down" state while it is being pressed or changing the color slightly while tapping on a list item, the visual feedback is good practice.