'Can't change backgroundTint programmatically
Android Studio 3.6
in my styles. xml
<style name="buttonStyle">
<item name="android:textColor">@color/default_button_textColor</item>
<item name="android:backgroundTint">@color/button_bg_color</item>
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
</style>
<style name="buttonClickStyle">
<item name="android:textColor">@color/button_bg_color</item>
<item name="android:backgroundTint">@color/button_click_bg_color</item>
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
</style>
in xml layout:
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonStartSearchBluetooth"
style="@style/buttonStyle"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_margin="@dimen/button_margin"
android:onClick="onClickButtonStartSearch"
android:text="@string/start_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
When click button I try to change style like this:
dataBinding.buttonStartSearchBluetooth.setTextAppearance(R.style.buttonClickStyle)
But it change only text color. Not change backgroundTint
Why?
Solution 1:[1]
try this code
yourbutton.setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
Solution 2:[2]
Use this code to change MaterialComponents Button Background tint color.
yourbutton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
Solution 3:[3]
setTextAppearance only take care about styling the Text of the TextView not the background. Check the official doc to see supported property that setTextAppearance affected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | raj kavadia |
| Solution 2 | Codemaker |
| Solution 3 | Md. Asaduzzaman |
