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.
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 am trying to very simply style a Button. I just want it blue with with text when not pressed, and white with blue text when clicked.
I tried to do this with a style and a selector.
In my layout I have this Button:
<Button
android:id="#+id/button1"
style="#style/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/login" />
And in res/values/styles I have this styles.xml:
<style name="MyButton">
<item name="android:background">#drawable/btn_background</item>
<item name="android:textColor">#drawable/btn_textcolor</item>
</style>
And of course, the two selectors, in res/drawable, btn_background.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="#color/white" />
<item android:color="#color/SapphireBlue" />
</selector>
and btn_textcolor.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="#color/SapphireBlue" />
<item android:color="#color/white" />
</selector>
The error I get now when I either run the app, or open the layout editor is:
<item> tag requires a 'drawable' attribute
I understand the message, but I don't have a drawable, is it is a simple, flat button.
How can I create such a simple button?
Update according to this post, it should work.
Try this way,hope this will help you to solve your problem.
btn_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_pressed="true"></item>
<item android:drawable="#drawable/white" android:state_focused="true"></item>
<item android:drawable="#drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/white" android:state_enabled="false"></item>
</selector>
btn_textcolor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/SapphireBlue" android:state_pressed="true"></item>
<item android:color="#drawable/SapphireBlue" android:state_focused="true"></item>
<item android:color="#drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="#drawable/SapphireBlue" android:state_enabled="false"></item>
</selector>
Background attribute expects a drawable and the drawable selector must be in one of the /drawable folders.
TextColor attribute expects a color and the color selector must be in one of the /color folders.
/res/color/my_textcolor_selctor.xml
/res/drawable/my_background_selector.xml
Also, the drawable selector must use 'android:drawable' for selector items (a color resource maybe used for the attribute value since a color is a drawable),
whereas the color selector must use 'android:color' for the selector items.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#color/disabledbackgroundColor" />
<!-- default -->
<item android:drawable="#color/myBackgroundColor" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:color="#color/disabledColor" />
<!-- default -->
<item android:color="#color/defaultColor" />
</selector>
android:background seems to accept drawable selectors but not color selectors... so just leave a color as your android:background, and use a normal color selector with android:backgroundTint, which expects colors anyway:
at styles.xml:
<style name="MyButton">
<item name="android:background">#color/some_fixed_color</item>
<item name="android:backgroundTint">#color/background_color_selector</item>
<item name="android:textColor">#drawable/btn_textcolor</item>
</style>
(res/color/background_color_selector.xml is a normal color selector.)
my source selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed" android:state_focused="true" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_selected" android:state_activated="true"></item>
<item android:drawable="#drawable/button_normal"></item>
</selector>
an example of how I set the style - button_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#F1F4F2" />
<corners
android:bottomLeftRadius="7dip"
android:topLeftRadius="7dip" />
</shape>
how in the "shape" set the size and color of the text?
maybe there is another way to set the size and color for the selector?
This way is just for the background of the button.
You should use another selector on the
android:textColor="#color/your_color_selector" and put this file under the res/color folder
Here is an example:
your_color_selector.xml
<item android:state_pressed="true" android:color="#android:color/blue"/>
<item android:state_enabled="true" android:state_selected="true" android:color="#android:color/white"/>
<item android:color="#android:color/black"/>
You can create a style, and that define the text size, color, and drawable (also much more).
You can do something like that:
Create a file named style.xml in the values folder:
<style name="button_style">
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">18dp</item>
<item name="android:background">#drawable/your_selector</item>
</style>
And then go to your Button declaration and define the style of a button for example :
<Button
style="#style/button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
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>
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>