airbnb / paris

Define and apply styles to Android views programmatically

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Applying style for MaterialButton

qbait opened this issue · comments

I'm trying to apply regular and outlined style to MaterialButton

    companion object {

        @Style
        val PRIMARY_STYLE = subscriptionRowStyle {
            buttonStyle {
                R.style.Widget_MaterialComponents_Button
            }
        }

        @Style
        val SECONDARY_STYLE = subscriptionRowStyle {

            buttonStyle {
                R.style.Widget_MaterialComponents_Button_Outlined
            }
        }

        @Style
        val DEFAULT_STYLE = PRIMARY_STYLE
    }

my attrs

    <declare-styleable name="SubscriptionRow">
        <attr name="buttonStyle" format="reference" />
    </declare-styleable>

However, it doesn't work. What am I doing wrong?

@qbait the following syntax is incorrect:

buttonStyle {
    R.style.Widget_MaterialComponents_Button
}

You can do:

buttonStyle(R.style.Widget_MaterialComponents_Button)

// Or

buttonStyle {
    add(R.style.Widget_MaterialComponents_Button)
    // This enables you to add more styles or override specific attributes if you need.
}

In my case, when I apply style button is not outlined:
<style name = "SexButtonSelected" parent="Widget.MaterialComponents.Button.OutlinedButton">
@color/red
</style>
<style name = "SexButtonNotSelected" parent="Widget.MaterialComponents.Button.TextButton">
@color/colorTitle
</style>

   boy_btn.style {
                        add(R.style.SexButtonSelected)
                    }
                    girl_btn.style {
                        add(R.style.SexButtonNotSelected)
                    }