0

I am trying to set the border and background, respectively black and white, of a button in Android. The border gets set, however the background of the button remains purple. What am I doing wrong with the background?

My shape file is called button_border_drawable.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="@color/white" />
    <stroke
        android:width="1dip"
        android:color="@color/black" />
</shape>

Here is my button

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/login_label"
    android:textColor="@color/black"
    android:background="@drawable/button_border_drawable">
</Button>

Theme:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Operator" 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>
        <!-- Customize your theme here. -->
    </style>
</resources>
Hendré
  • 262
  • 10
  • 27

2 Answers2

2

If you want to change the background color and stroke color, I recommend you to use material button rather than editing your theme

    <com.google.android.material.button.MaterialButton
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cornerRadius="0dp"
    app:strokeColor="@color/black"
    app:strokeWidth="1dp"
    android:text="@string/allow"
    android:textColor="@color/black"
    app:backgroundTint="@color/white"/>

Here you can control background color, strok color/width and corner radius,

also if you want to remove defual padding from material button you can set inset to 0

    android:insetBottom="0dp"
    android:insetTop="0dp"
    android:insetRight="0dp"
    android:insetLeft="0dp"

also don't forget to add material library dependency in gradle

api 'com.google.android.material:material:1.5.0'
//api make it visible to other library depending on this module if this is in library
Islam Assem
  • 1,376
  • 13
  • 21
0

The issue was solved by using the Theme.AppCompat.DayNight theme.

Hendré
  • 262
  • 10
  • 27