0

I am trying to change my positive button of the alert dialog. Can someone please help me?

This is what I have tried so far in my theme.xml from reading other people's answer:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyFirstTest" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/yellow_200</item>
        <item name="colorPrimaryVariant">@color/yellow_500</item>
        <item name="colorOnPrimary">@color/black</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>
        <!-- Customize your theme here. -->
        <item name="android:positiveButtonText">#00f</item>
    </style>
    <style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
        <item name="buttonBarPositiveButtonStyle">@style/Alert.Button.Positive</item>
    </style>

    <style name="Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="android:textColor">#00f</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textAllCaps">false</item>
    </style>

</resources>

I am trying to change the positive button color to blue (#00f), but it kept on showing my primary color (yellow_200). I honestly do not know how to customize this theme: Theme.MaterialComponents.Light.NoActionBar Is there a website that guides what is included in this theme and how to customize it?

Thank you!

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • Does this answer your question? [How can I change default dialog button text color in android 5](https://stackoverflow.com/questions/27965662/how-can-i-change-default-dialog-button-text-color-in-android-5) – SebastienRieu Jun 28 '21 at 10:05
  • I followed those before, but my positive button is still yellow. – Brian McCanaugh Jun 28 '21 at 13:59

1 Answers1

0

There is some things that are currently wrong. You need to update your styles as follows to change the text color

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#f00</item>
</style>

<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#00f</item>
</style>

You can find more information regarding material components and their theming on their website as follows

https://material.io/components/dialogs/android#theming-dialogs

che10
  • 2,176
  • 2
  • 4
  • 11
  • I tried this previously, but the color of my positive button is still yellow. – Brian McCanaugh Jun 28 '21 at 13:59
  • @BrianMcCanaugh Change buttonBarNegativeButtonStyle to android:buttonBarNegativeButtonStyle and buttonBarPositiveButtonStyle to android:buttonBarPositiveButtonStyle – che10 Jun 29 '21 at 04:03
  • My android studio cannot process the "android:buttonBarNegativeButtonStyle". It suggests to override Resource in values-v21 to avoid the error. Any other ideas? Thank you for your help. – Brian McCanaugh Jun 29 '21 at 05:37
  • What is the minSdk version of your app @BrianMcCanaugh – che10 Jun 29 '21 at 05:39
  • My minSDK is 16. – Brian McCanaugh Jun 29 '21 at 06:28
  • @BrianMcCanaugh Then override it in v21 as this will work using that only on API level 21+ I am assuming your current device is running API 21+ that is why it is not working. – che10 Jun 29 '21 at 08:32
  • Yes correct, I am running API28. If I override the v21, is there anything that I should be aware of? Like will my other color changes as well? (i.e. the snackbar, etc.) – Brian McCanaugh Jun 29 '21 at 09:16
  • I do not thing the other colors will change. How overriding works is the properties you override get picked from v21 file and others get picked from the default theme. And since we are only changing the buttons so I do not think it will impact much. @BrianMcCanaugh If you add any other properties to your base theme in future and see it is not working, just remember to add them to your v21 – che10 Jun 29 '21 at 09:34