1

i'm trying to customize my alert dialog. but i can not change the background color of buttons. i tried drawable button designing, style, setting direct color in xml and also setting color by java codes in the activity but i could not change it. the shape of the button changes but the color only can be either the default color or transparent color.

the xml coding of button itself:

<Button
                android:id="@+id/btnDenied"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginTop="15dp"
                android:layout_marginEnd="35dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/button_selected" //// or @drawable/button_costume
                android:text="@string/button_no" />

the selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed = "true">
        <shape>
            <solid android:color="#FFEB3B"/>

        </shape>
    </item>

    <item android:state_pressed = "false">
        <shape>
            <solid android:color="#FFC107"/>

        </shape>
    </item>

</selector>

and another drawable file:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="50dp"/>
    <solid
        android:color="@color/colorHeader"/>
</shape>

and i also tried this style:

<style name="Button_Style"
        parent="@android:style/Widget.Button">
        <item name="android:textSize">14sp</item>
        <item name="android:background">@color/colorHeader</item>
        <item name="android:textColor">#1B1B22</item>
        <item name="android:textStyle">bold</item>
        <item name="android:layout_margin">2dp</item>
    </style>

but non of them could change the color of the button.

  • 1
    Your question is not clear. are you trying to change the button in your alert dialog? – Gabriele Mariotti May 30 '20 at 09:08
  • yes. i want to change the color of the buttons in alert dialog. but i tried multiple ways to do this and nothing changed. i changed something to have a transparent button one month ago and i can not remember it exactly now the app ignores any changes in color of buttons. – Mohsen Mashkour Jun 02 '20 at 11:43
  • 1
    Check this: https://stackoverflow.com/questions/57053520/change-dialog-button-color/58043299#58043299 – Gabriele Mariotti Jun 02 '20 at 11:50

1 Answers1

1

The code that I have written is 100 percent correct, but sometimes Android Studio does some wrong tasks, so solve these issues either by running the program again, or doing the following steps:

  • Clean Project , From the build menu .

  • Rebuild Project .

And I got this problem with me not to show errors in xml file, I hope this thing helped you.

Adel B-Lahlouh
  • 1,059
  • 1
  • 8
  • 20