Search code examples
androidandroid-buttonandroid-themeandroid-stylesmaterial-components-android

Change Color from material outlined button doesn't work


I want to change the color of the active toggle button. But just the change of the rippleColor makes a difference. I wish to customize the background color of the active button and the text color.

<style name="ToggleButtonGroupStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
            <item name="rippleColor">@color/colorAccent</item>
</style>

In the following toggleButtonGroup I used this style from above:

         <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/priority_btn_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentLeft="true"
            app:selectionRequired="true"
            app:singleSelection="true"
            app:checkedButton="@+id/btn_one"
            >

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn_one"
                style="@style/ToggleButtonGroupStyle"
                android:layout_width="@dimen/priority_btn_width"
                android:layout_height="wrap_content"
                android:shadowColor="@color/project_text"
                android:text="0" />

            <com.google.android.material.button.MaterialButton
                style="@style/ToggleButtonGroupStyle"
                android:layout_width="@dimen/priority_btn_width"
                android:layout_height="wrap_content"
                android:text="!" />

            <com.google.android.material.button.MaterialButton
                style="@style/ToggleButtonGroupStyle"
                android:layout_width="@dimen/priority_btn_width"
                android:layout_height="wrap_content"
                android:text="!!" />

            <com.google.android.material.button.MaterialButton
                style="@style/ToggleButtonGroupStyle"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="!!!" />


        </com.google.android.material.button.MaterialButtonToggleGroup>

Could anybody give me a hint what the problem here is? Thanks :)


Solution

  • The background color of the checked buttons is based on the colorPrimary attribute.
    You can use:

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_one"
        style="?attr/materialButtonOutlinedStyle"
        android:theme="@style/ThemeOverlay.Custom.Button"
    

    with:

    <style name="ThemeOverlay.Custom.Button" parent="">
        <item name="colorPrimary">@color/....</item>
    </style>
    

    enter image description here

    or you can use a custom style with:

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_one"
            style="@style/ToggleButtonGroupStyle"
    

    with:

    <style name="ToggleButtonGroupStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
        <item name="backgroundTint">@color/custom_selector</item>
    </style>
    
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/..." android:state_checked="true"/>  <!-- selected color -->
        <item android:color="@android:color/transparent" android:state_checked="false"/>
    </selector>
    

    enter image description here enter image description here