Search code examples
androidandroid-studioandroid-layoutandroid-viewandroid-theme

Android theme overrides all colors


I have a XML file that defines the Apps UI with colors and drawables but they all seem to get overridden by the theme from themes.xml. How do I disable this?

Example:

This button:

<Button
            android:id="@+id/capture_button"
            android:layout_width="match_parent"
            android:background="@drawable/round_button"
            android:layout_height="0dp"
            android:text="@string/capture_button_text_en"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/camera_activity_frameLayout">
</Button>

Should have a black background as it is defined in round_button.xml:

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

However it is purple, the color from the theme:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.myproject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- 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>

How do I override this theme?


Solution

  • Since your application's theme is extending a Material Components theme (Theme.MaterialComponents.DayNight.NoActionBar), it will take over the view inflation and replace some widgets with the corresponding Material Components one. This means, that if your views define a Button, you get a MaterialButton and as a consequence it changes how you should control your button's appearance. You should no longer use the background attribute, but the attributes discussed here and here. This process is explained a bit more in this blog post.

    Now to get a black MaterialButton with a white outline, you want to use the Widget.MaterialComponents.Button.OutlinedButton style:

    <Button
        android:id="@+id/capture_button"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="@string/capture_button_text_en"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:shapeAppearance="@style/ShapeAppearance.MyApp.LargeComponent"
        app:backgroundTint="@color/black"
        app:strokeWidth="1dp"
        app:strokeColor="#ffffff"
        app:layout_constraintTop_toBottomOf="@+id/camera_activity_frameLayout"/>
    

    You also need the following in your styles.xml to make the corners rounded with 7dp corner radius (as seen from your original background drawable):

        <style name="ShapeAppearance.MyApp.LargeComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
            <item name="cornerFamily">rounded</item>
            <item name="cornerSize">7dp</item>
        </style>