How can I replace <background android:drawable="@color/ic_meter_launcher_background"/> with a value in Android Studio?

Viewed 130

Code A is generated automatically by Android Studio.

I hope to replace <background android:drawable="@color/ic_meter_launcher_background"/> with a color value, but both Code B and Code C are wrong.

How can I do?

Code A

ic_launcher.xml file

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@color/ic_meter_launcher_background"/>
    <foreground android:drawable="@drawable/ic_meter_launcher_foreground"/>
</adaptive-icon>

ic_meter_launcher_background.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="ic_meter_launcher_background">#3DDC84</color>
</resources>

Code B

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@color/#3DDC84"/>
    <foreground android:drawable="@drawable/ic_meter_launcher_foreground"/>
</adaptive-icon>

Code C

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="#3DDC84"/>
    <foreground android:drawable="@drawable/ic_meter_launcher_foreground"/>
</adaptive-icon>
2 Answers

You can override any resource of the Android app and even the framework itself:

<color tools:override="true" name="ic_meter_launcher_background">#3DDC84</color>

where tools should be a xmlns:tools="http://schemas.android.com/tools" in your resources XML file header.

You can do it like this:

ic_launcher.xml file

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_meter_launcher_background"/>
    <foreground android:drawable="@drawable/ic_meter_launcher_foreground"/>
</adaptive-icon>

ic_meter_launcher_background.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="108dp"
    android:height="108dp"
    android:viewportWidth="108"
    android:viewportHeight="108">
    <path
        android:fillColor="#3F3F4A"
        android:pathData="M0,0h108v108h-108z" />
</vector>
Related