I am trying to set up a selector for TextView textColor using the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="?android:attr/textColorTertiary" />
<item android:color="?android:attr/textColorPrimary"/>
</selector>
However, the color always appears to be red instead of those theme colors. If I put hardcoded color, everything seems to work fine. (ex. <item android:state_enabled="false" android:color="#666666" /> ).
What is the problem and how to solve it? P.S. if anyone knows how to set theme's default disabled color for disabled item in the list, please share, that is what I am trying to achieve. Thanks.
As far as i can see you may have to use 3 states in a selector.
state enabled
state focused
state pressed
in exactly this order. This might help
You used selector for what reason?
If you want to make your text of text view always red then no need of selector. Just define color in color.XML or in string.XML using add color.
And if you want to chanhe it on selection or focus than use the states.
state enabled
state pressed
state focused
Than it will work as you need.
Related
I have an unusual problem in my application. I am using Material Spinners' library and when I select an item from dropdown menu, I get this weird orange background color.
Example:
I have exact same problem on my Caldroid calendar and it only happens for the background color when I click an item. I don't have this color set in my values and I don't know what to do. I need to change this color to something that is more suitable for my application.
I tried changing the colorPrimary, colorHighlighted and stuff like that in the AppTheme but it was no use.
Any help is very much appreciated!
You can do it by creating a custom selector and set as background of spinner.
First create xml file custom_slector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#color/selected_item_color" />
<item android:drawable="#android:color/default_color" />
</selector>
Then set this as your background in spinner
android:background = "#drawable/custom_selector"
android:drawSelectorOnTop = "true"
I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..
I try to change the color based on my theme. My TextView is using color-selector with different states for enabled and disabled and I want to use my theme based color in this selector.
I have followed this solution: android themes - defining colours in custom themes
My selector used as android:textColor in my view looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#ffffff" />
<item android:state_enabled="false" android:color="?attr/ThemeTest"/>
</selector>
with ThemeTest being my custom attribut which has a color assigned in my themes. If I use this selector as my textColor, the color is actually not what I picked but just a simple plain RED! HOWEVER if I use the custom attribut directly in my view
android:textColor="?ThemeTest"
then it works but I obviously want to do this based on the change of state of my view...
Does anybody understand this behaviour and know how to fix it? Thanks in advance!
Using a theme attribute inside a color selector XML file is only supported in the most recent versions of Android. To overcome this limitation you need to create one color selector file for each theme, and fill them with plain colors. Then create a theme attribute which points to the correct color selector depending on the theme.
source: https://plus.google.com/102404231349657584821/posts/XEeehfwanGy
edit: tested and it works flawlessly!
I have already been using selector drawables to make my button change background according to the state.
However, I also want to change the text color and left compound drawable together with the background. But the default selector XML atrribute does not contain any "android:textColor" or "android:drawableLeft" to be assigned.
I know I can always achieve this with extend my own button class, but is there any clean way out?
I am not very sure about drawables but for changing textcolor depending upon button state, I use selectors as below,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#color/color_light_green"></item>
<item android:color="#fff"></item>
//state you want
</selector>
and then apply it to textColor attribute in the xml as,
android:textColor="#drawable/selector_btn_text_color"
Eclipse doesn't auto suggest color attribute in selector but we can do it. :)
I'm trying to achieve that an icon in ActionBar will not change states discretely, but by fading animation. When I add android:enterFadeDuration and android:exitFadeDuration to the selector tag, my drawable is initially invisible - when I tap it, it changes state to state_pressed (properly with enter fade duration) and when I release it, it turns back to its normal visible unselected state.
I must be missing something obvious, or is this a bug of some kind?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="150" android:exitFadeDuration="150">
<item android:drawable="#drawable/filters_toggle_icon_selected" android:state_focused="true"/>
<item android:drawable="#drawable/filters_toggle_icon_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/filters_toggle_icon" android:state_focused="false" android:state_pressed="false"/>
</selector>
I had a similar problem, with my code looking like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="#android:integer/config_mediumAnimTime"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#color/pressed" />
<item android:drawable="#color/default" />
</selector>
At first, I found a hint to get rid of enterFadeDuration and only use exitFadeDuration. That solved the problem with initial invisibility, but the view still faded into invisibility during the first interraction.
Then, I modified my structure as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/default" />
<item>
<selector android:enterFadeDuration="#android:integer/config_mediumAnimTime"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#color/pressed" />
</selector>
</item>
</layer-list>
Basically, I just pushed the default drawable out of the selector. It's a workaround and it also works for selectors with multiple states, but has some notable limitations:
The default drawable is always visible as a bottom layer. It works for opaque colors, but transparency may cause undesirable results.
If the view starts in one of the states tested by selector, in still displays as default, because the selector still starts as invisible.
It might not be applicable to the original problem, but it's something to consider for overcoming this behaviour of selectors.
Use android:enterFadeDuration="#android:integer/config_mediumAnimTime" and android:exitFadeDuration="#android:integer/config_mediumAnimTime".
My problem was similar as well, the issue was that after setting a background drawable on my view, it was in the wrong state (sometimes it even mixed the stroke and solid of two states...). This was only before the first interaction, like receiving focus or changing enabled state.
I've found that if you call jumpToCurrentState() on the drawable (which has the fade duration properties) after setting it on the view, it will be set on the correct state, and you can keep on using the enter / exit fade duration properties.
Here's how I did it:
val stateList = (darkBackground as? RippleDrawable)?.findDrawableByLayerId(android.R.id.background) as? StateListDrawable
background = darkBackground
stateList?.jumpToCurrentState()
In this example I had a ripple drawable which contained the selector which had the fade properties (I had to add an ID to the selector so I could look it up using the ID).
This seems to be a bug that happens on specific Android versions. You can turn off the android:enterFadeDuration programmatically in Java code, by accessing the Selector with a StateListDrawable:
// Disable android:enterFadeDuration/exitFadeDuration on Android 4.2 only
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
StateListDrawable stateListDrawable =
(StateListDrawable) animatedButton.getBackground();
stateListDrawable.setEnterFadeDuration(0);
stateListDrawable.setExitFadeDuration(0);
}