Issue
I have two buttons, and i want to realize blue border on pressed one. When button pressed, it't border become blue, border of another button return to default.
I can't return default border correct. I write function, but is works wrong.
My button(second is the same with another id and text):
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_schemes_1"
style="@style/Buttons.Schemes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_4"
android:layout_weight="1"
android:text="@string/schemes_1"
app:icon="@drawable/ic_schemes_1" />
Style has nothing interesting:
<style name="Buttons.Schemes" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:typeface">sans</item>
<!--android:fontFamily="sans-serif-condensed-medium"-->
<item name="android:textColor">@android:color/black</item>
<item name="android:letterSpacing">0</item>
<item name="iconTint">@null</item>
<item name="backgroundTint">@android:color/white</item>
</style>
My last realization was about to find and remember default border value before first click happens, so at the first i write:
private var defaultColor: Int = 0
Then
defaultColor = btn_schemes_1.strokeColor.defaultColor
And function, called when one of button clicked:
private fun setButtonsBorder(buttonNumber: Int) {
when (buttonNumber) {
1 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_1.strokeWidth = 2
btn_schemes_2.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_2.strokeWidth = 1
}
2 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_1.strokeWidth = 1
btn_schemes_2.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_2.strokeWidth = 2
}
}
}
But this failed too, because default color not "those" default at fact. When i set it, i found it's just colorPrimary
.
How can i find this default color or write method to retutn border color to default?
Solution
Ok, i get it myself.
After goes deeper i call it before button clicked:
Timber.e(btn_schemes_1.strokeColor.toString())
And get:
ColorStateList{mThemeAttrs=nullmChangingConfigurations=1073742848mStateSpecs=[[16842912], [-16842912]]mColors=[-16743049, 520093696]mDefaultColor=-16743049}
So 520093696 is my color. Google says it black12 constant
, presented by 0x1F000000
And after
private var defaultColor: Int = 0x1F000000
All works.
Answered By - KirstenLy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.