I want to change a button's color programmatically by assigning its background to another xml file that customizes the button. However, when running the program my button stays the color that I set as my color primary variant in my themes xml file. Here is my button:
<Button
android:id="@+id/colorButton"
android:layout_width="45dp"
android:layout_height="43dp"
android:background="@drawable/custom_button_color_picker"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
As you can see I set the background to a drawable xml file called custom_button_color_picker. Here is the xml file that I set as the background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp"/>
<solid android:color="@color/white"/>
</shape>
Here is the part of my theme xml file where I set my primary variant color (automatically changing all button colors to this color):
<item name="colorPrimaryVariant">@color/dark_navy</item>
Here is my themes.xml file
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WeatherApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/navy</item>
<item name="colorPrimaryVariant">@color/light_navy</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/cream</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:fontFamily"> @font/gilroy_extra_light</item>
</style>
<style name="Theme.WeatherApp.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.WeatherApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.WeatherApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
In my custom_button_color_picker xml file I assigned the color white to the button. However, when I run my program, the color of the button remains the dark navy color (my primary variant color). Is this because the primary variant theme color overrides any color changes to the button? If so, how can I override the primary variant color? If not, where did I make my mistake? Please feel free to request any additional information about my code.
Thanks in advance