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?
Related
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 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 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.
I have an EditText control in my application and when it is focused (i.e. I'm ready to enter something) I want to apply shadow color to it. How can I do this?
Thanks,
#nagaraju.
You can use the Selector tag like Follows:
Create a New xml file and place it in your drawable folder and name it as shadow_color.xml
<?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="false" android:drawable="#drawable/ask_footer"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/askfooter_hover" />
<item android:drawable="#drawable/ask_footer" />
</selector>
And then go to that xml in which your EditText is declared:
And write one attribute in editText
android:background="#drawable/shadow_color"
and you are done.
Use a selector for your background drawable and create your own nine patches.
does shadow color mean you want a shadow theme background for your view??
then, assuming that you are using xml layout,the GU interface version shown on eclipse allows you to select a particular theme for your view,which i think is what you want.
if you want to put it manually,
you must mention it in the android manifest
eg.
<activity android:name=".activity_name here"android:theme="#android:style/Theme.Dialog">//there are a handful of effects provided by SDK
</activity>
i suggest the graphical user interface method,its easy and you can see the effect as you select the theme
You can also check whether your EditText isFoucsed or not. And if true so change its background image to shadow like image.
In my project I am not able to change the color of the hyperlink text.
Basically this text is shown in TextView and for it I have used xml layout such as
android:autoLink="all"
Now the problem is that I want to change the color of this hyperlink text to white
Thanks all in advance.
use this property android:textColorLink="#android:color/white" for that textView
Don't know if you still need to know this, put it here in case other people want to know. If you want to change the link text color when it is clicked, just try this:
1 create res/color folder, create linkcolor.xml in this folder
2 put these code in linkcolor.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="#FFFF0000"/>
<item android:state_focused="true" android:color="#FFFF0000"/>
<item android:color="#FF00FF00"/>
</selector>
3 for your TextView, just add:
android:textColorLink="#color/linkcolor"