Can't change backgroundTint programmatically
K

4

6

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?

Kilgore answered 27/10, 2019 at 15:22 Comment(0)
P
10

To change programmatically the text color and the background color use:

  • the method setBackgroundTintList to change the background color selector.
  • the method setTextColor to change the text color

Something like:

    button.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color.button_selector));
    button.setTextColor(ContextCompat.getColor(this, R.color....));

In any case there are some issues in your style.

  • use a MaterialComponents.Button.* style as parent
  • use backgroundTint instead of android:backgroundTint
  • use android:textAppearance to define your text style

Something like:

<style name="buttonStyle" parent="@style/Widget.MaterialComponents.Button">
  <item name="android:textColor">@color/default_button_textColor</item>
  <item name="backgroundTint">@color/my_selector</item>
  <item name="android:textAppearance">@style/my_textAppearance</item>
</style>
<style name="my_textAppearance" parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textSize">18sp</item>
    <item name="android:textAllCaps">true</item>
</style>
Phanerogam answered 27/10, 2019 at 16:14 Comment(2)
You forgot to use "button.setTextAppearance(R.style.buttonStyle)" to change text color of button "on fly"Kilgore
@a_subscriber In your question you asked how to change the background color. In any case use button.setTextColor(ContextCompat.getColor(this, R.color....));.Phanerogam
V
8

Use this code to change MaterialComponents Button Background tint color.

yourbutton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
Vorster answered 18/2, 2022 at 11:8 Comment(1)
this is the answer!Hump
A
3

try this code

yourbutton.setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
Acerb answered 27/10, 2019 at 15:29 Comment(0)
R
0

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.

Redeemer answered 27/10, 2019 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.