0

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?

roflcopter1101
  • 283
  • 1
  • 6
  • 20
  • 2
    If you're using MaterialComponents (which your theme is) then you probably want to set `materialButtonStyle` instead. The Material Design docs are probably more useful to you, it has its own theming system: https://material.io/develop/android/theming/color (scroll down to the Theming All Instances bit) – cactustictacs May 29 '22 at 16:56

3 Answers3

2

You have to use materialButtonStyle instead of buttonStyle.

<item name="materialButtonStyle">@style/ButtonColour</item>
Diniz
  • 155
  • 1
  • 4
0

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.

tintin
  • 335
  • 2
  • 8
-1

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.

Yazan
  • 76
  • 10
  • This isn't really a fix.. the proper way to set the background colour is by using the backgroundTint attribute, not just setting the background picture to a solid colour.. a proper fix has been given in the comments of the question. – roflcopter1101 May 30 '22 at 00:11