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. :)
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 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!
Created a preference file for settings in android. When I uncheck a CheckBoxPreference I want its dependent preferences to turn out to grey color (i.e to potray). I had tried using, setShouldDisableView() but its not working. Suggest me the idea to greyout the dependent Checkboxpreference's (title and checkbox), but in default only the color of checkbox is changing.
Create separate xml file in drawable folder and paste the below code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#D3D3D3" android:state_checked="true"/>
<item android:drawable="#ffffff" android:state_checked="false"/>
</selector>
This xml file name set as android:background="#drawable/filename" in your check box tag xml. Hope this will help you!
In your theme use selectors for primary/secondary colors instead of using "pure" colors. See also: Android: change color of disabled text using Theme/Style?
I have some TextViews which are dinamically added to a LinearLayout. These TextViews are clickable and have an onLongClickListener (I also intend to add onClickListener later)
Here is the thing, I want these TextView to change their background color when pressed and I read that you can use selectors to do such thing.
So I made this xml file in res/drawable/text_view_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000"/>
<item android:state_pressed="false"
android:color="#FFFFFF"/>
</selector>
I tried to create a TextView and using this xml file likes this:
TextView t = new TextView(this);
t.setBackgroundColor(R.drawable.text_view_pressed);
But when I do this, it will give this error in t.setBackgroundColor: "Should pass resolved color instead of resource id here: getResources().getColor(R.color.text_view_pressed)" but it doesn't work as intended if I use getResources().getColor(R.color.text_view_pressed).
Anyone got an idea how to do it?
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList and StateListDrawable.
A color state list can only be used in certain contexts, for example in TextView.setTextColor(). As far as I can see, you cannot use a color state list as parameter of setBackgroundColor() if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable attribute is mandatory.
So, to sum up:
The xml file should be placed in res\drawable,
Its structure should be slightly different (i.e. state list, not color list), and
You need to use setBackgroundResource() instead of setBackgroundColor().
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values and reference them from here.
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.