skip to Main Content

I’ve created a brand new app using the basic activity. I want to change the colour of all buttons (from the default primary colour of purple to a custom non-primary colour of green). Here’s my themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="buttonStyle">@style/ButtonColour</item>

        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.MyApplication.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="ButtonColour" parent="Widget.MaterialComponents.Button">
        <item name="backgroundTint">#689f38</item>
        <item name="rippleColor">#c5e1a5</item>
    </style>
    <style name="Theme.MyApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.MyApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

where I apply the ButtonColour style, as defined in the android dev docs here: https://developer.android.com/guide/topics/ui/look-and-feel/themes#Widgets
When the app is ran with that custom style to change the buttons to green, the buttons are still the default primary colour.

I’ve read other questions (1, 2, and 3) but none of their solutions helps either. Does anyone have any guidance on how this can properly be done?

3

Answers


  1. You have to use materialButtonStyle instead of buttonStyle.

    <item name="materialButtonStyle">@style/ButtonColour</item>
    
    Login or Signup to reply.
  2. you can create a new layout resource file inside the drawable folder in the resources and its gonna be a shape

    inside this XML file, you’re gonna be able to edit your button style like so:`

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <solid
        android:color="@color/antique_white"/>
    
    </shape>
    

    `
    you can change the color inside <solid.
    now, you need to use this XML file as a background for all your buttons as so:
    let’s say you named the XML file button_style

    android:background="@drawable/button_style"
    

    now you can just edit the XML file and the changes would apply to all you buttons.

    Login or Signup to reply.
  3. Change

    <style name="ButtonColour" parent="Widget.MaterialComponents.Button">
    

    to

    <style name="ButtonColour" parent="Theme.MyApplication">
    

    In Button:

    <Button style="@style/ButtonColour"
    

    as you have already used materialtheme as parent theme for app. Button, by default inherits the material theme properties.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search