The rows of my list have a custom selector. The background for this selector is color A when not pressed and color B when pressed. A TextView on the item's layout is colored B for juxtaposition when the list item is not pressed.
However, I would like the text color to change to color A while the row is being pressed, to juxtapose with the new background color.
How do I accomplish this?
You can style your text with a textColor drawable:
<style name="listLabel">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#drawable/list_selector_text</item>
</style>
and drawable/list_selector_text looks like this:
<?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="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/offwhite" />
<item android:state_pressed="true" android:color="#color/white" />
<item android:color="#color/black" />
</selector>
Related
I programmatically Chips (Material Components), use setChipBackgroundColor change chip state press color but it's have two press color effect, i want to remove default press gray color
my custom press (#daecff)
default press color (gray)
thanks
https://i.imgur.com/5z94oUA.jpg
Chip chip = new Chip(context);
chip.setText(name);
chip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
chip.setTextColor(ContextCompat.getColor(context, R.color.chip_color));
chip.setOnClickListener(onClickListener);
chip.setChipStrokeColorResource(R.color.chip_color);
chip.setChipStrokeWidth(5);
chip.setChipBackgroundColor(
ContextCompat.getColorStateList(context, R.color.bg_chip)
);
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#daecff"/>
<item android:color="#color/white"/>
</selector>
You have to use the setRippleColor method:
chip.setRippleColor(ContextCompat.getColorStateList(this,R.color.my_selector));
with something like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="#dimen/mtrl_low_ripple_pressed_alpha" android:color="?attr/colorPrimary" android:state_pressed="true"/>
<item android:alpha="#dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorOnSurface" android:state_focused="true" android:state_hovered="true"/>
<item android:alpha="#dimen/mtrl_low_ripple_focused_alpha" android:color="?attr/colorOnSurface" android:state_focused="true"/>
<item android:alpha="#dimen/mtrl_low_ripple_hovered_alpha" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="#dimen/mtrl_low_ripple_default_alpha" android:color="?attr/colorOnSurface"/>
</selector>
Also you have to check the selector used in the setChipBackgroundColor method, in particular the color used for the selected state:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 24% opacity -->
<item android:alpha="0.24" android:color="#color/custom" android:state_enabled="true" android:state_selected="true"/>
....
</selector>
I have a custom selector to Thumb and Track Switch
switch_thumb_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bt_rules_box_control" android:state_enabled="false" />
<item android:drawable="#drawable/bt_rules_box_control" android:state_pressed="true" />
<item android:drawable="#drawable/bt_rules_box_control" android:state_checked="true" />
<item android:drawable="#drawable/bt_rules_box_control" />
</selector>
switch_track_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_rules_box_control" android:state_enabled="false" />
<item android:drawable="#drawable/bg_rules_box_control" android:state_pressed="true" />
<item android:drawable="#drawable/bg_rules_box_control" android:state_checked="true" />
<item android:drawable="#drawable/bg_rules_box_control" />
</selector>
I want to get this Switch like result and put text ON/OFF
My question is how can i change the color of the ON / OFF text without changing the thumb image? The color ON / OFF text is white and the image color of the thumb is same. It confuses the two colors
You can set the text color to a color state list selector. Use the same states as before, but use android:color instead of android:drawable as the value for each state.
Color StateList
Notice that this resource goes in res/color, not res/drawable
res/color/switch_text_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00000000" android:state_checked="true" />
<item android:color="#FFFFFFFF" />
</selector>
To create a custom ToggleButton, I've defined a new style in /res/values/styles.xml:
<style name="myToggleButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:background">#drawable/my_toggle_button</item>
</style>
and I then use a selector to specify how the button's states look in /res/drawable/my_toggle_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
[...]
</shape>
</item>
<item android:state_checked="false"
<shape>
[...]
</shape>
</item>
</selector>
How can I modify this setup to toggle the text color of the button when the state changes?
Create a similar state list for the text colors you would like, and place it in res/color, e.g.
res/color/toggle_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#070" />
<!-- Default State -->
<item android:color="#A00" />
</selector>
Then set this resource as the text color of the button:
<item name="android:textColor">#color/toggle_color</item>
P.S., it's good practice to have the last item in a selector not have any state flags attached (i.e. a default state) rather than defining it with the inverse of the above states.
Button selectors allows to define a background for each state of the button, but how can the button's text style be defined for each state? My objective is to grey out a button when disabled. So I set a greyed out background to the disable state of the button, but the only way I found to also grey out the text is to change it's color dynamically when I disable the button...
Anybody can help?
Thanks
<style name="srp_button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_default</item>
<item name="android:textColor">#ffffff</item> <!-- How to specify different color for different state? -->
<!-- more stuff -->
</style>
drawable/btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"><shape>
<solid android:color="#color/pink" />
<corners android:radius="6dp" />
</shape></item>
<item><shape>
<solid android:color="#000000" />
<corners android:radius="6dp" />
</shape></item>
</selector>
Well!
You can define the Text color for all 4 state, similarly as you defined background for the Button. For example:
file name: /res/color/text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#440000"/>
<item android:state_focused="true" android:color="#004400"/>
<item android:state_pressed="true" android:color="#000044"/>
<item android:color="#444"/>
</selector>
Take this file into your Button Style like this:
<style name="srp_button" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/btn_default</item>
<item name="android:textColor">#color/text_color</item> <!-- Here is specified text color -->
</style>
How to change color of the text in TextView on hover like in css with selector? I know for button to define selector with items
<item
android:drawable="#drawable/down"
android:state_pressed="true"
android:state_enabled="true">
</item>
<item
android:drawable="#drawable/up"
android:state_focused="true"
android:state_enabled="true">
</item>
but I need for TextView textColor, but item doesn't recognize that attribute. Is there way to do this from xml and not from code
?
Add that selector as a resource file (res/color/text_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:color="#color/focused_text_color"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#color/pressed_text_color" />
<item android:state_enabled="false" android:color="#color/disabled_text_color" />
<item android:color="#color/default_text_color"/>
</selector>
And use it:
android:textColor="#color/text_color"