As shown below, I was able to implement textColor only by using Color State List.
In the same way, is there a way to change text and BackgroundColor depending on the condition?
res/color/button_text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#00000000"/>
<item android:state_focused="false" android:state_pressed="true" android:color="#000000"/>
<item android:color="#ffffff"/>
</selector>
res/layout/example.xml
...
<Button
android: textColor="@color/button_text_color"/>
...
I don't know how to change the text depending on the button condition.
For background colors, create an xml of the selector
and set it to background
.
res/drawable/bg_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_window_focused="false">
<color android:color="#ff8888"/>
</item>
...abbreviated
</selector>
res/layout/example.xml
<Button
android:background="@drawable/bg_button"/>
However, if only the color is specified, it is simply a solid coating, and it does not look like a button.
If you choose a color after making it button-like, you can prepare an image with the color or shape you want, or use shape
to display it beautifully.
Example of specifying an image (you may want to refer to sdk's btn_default.xml, etc.)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal"/>
...abbreviated
</selector>
shape
Specify Example
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="#ff9999"/>
<corners android:radius="4dp"/>
<stroke
android:width="1dp"
android:color="#ff8888"/>
<padding
android:left="8dp"
android:right="8dp"/>
</shape>
</item>
...abbreviated
</selector>
API Level 21
and later, if you specify above, riple
will no longer appear, so
If so, use <ripple>
.
If you are using an image, use <riple>
when state_pressed="ture"
to specify the rippling color, and then specify the image within it.
<item
android:state_pressed="true">
<ripple android:color="#ffccccc">
<item android:drawable="@drawable/btn"/>
</ripple>
</item>
shape
specifies the ripples color with <ripple>
just like the image, and defines <shape>
in it.
<item
android:state_pressed="true">
<ripple android:color="#ffccccc">
<item>
<shape android:shape="rectangle">
<solid android:color="#ff9999"/>
<corners android:radius="4dp"/>
<stroke
android:width="2dp"
android:color="#ff8888"/>
<padding
android:left="8dp"
android:right="8dp"/>
</shape>
</item>
</ripple>
</item>
API Level 21
or later, if you only want to change the button color, you can also specify it in the theme.
<style name="AppTheme" parent="android:Theme.Material.Light">
<!--Button background color-->
<item name="android:colorButtonNormal">#88ff88</item>
<!--- The button is the color when pressed.Other than buttons -- >
<item name="android:colorControlHighlight">#993333</item>
</style>
Nice to meet you.
Background is a bit different.
If you define this in the background attribute, it will be reflected.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<color android:color="#ff0000"/>
</item>
<item android:state_selected="false">
<color android:color="#00ff00"/>
</item>
</selector>
© 2024 OneMinuteCode. All rights reserved.