27

So I have been working on this app for a couple weeks and I started to build the icon. I have Android Studio 3.0.1 and they seem to have changed the way Image Assets are made, now they have adaptive icons. I made an icon with transparent background for my app. Before, I would just change the shape to "none" and there would be no background generated. But now that is not an option unless I go to "legacy" which is useless. The background color does not seem to support transparency. Even if in the ic_launcher.xml I set the background to a transparent color but the icon still appears with a black background.

Here is my ic_lancher.xml

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

And ic_launcher_round.xml is the same. The @color/transparent bit is a this:

<color name="transparent">#00000000</color>
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
JoseRivas1998
  • 433
  • 1
  • 6
  • 14
  • 1
    Did you check this :- https://stackoverflow.com/questions/37085753/android-studio-image-asset-launcher-icon-background-color and https://medium.com/google-developers/implementing-adaptive-icons-1e4d1795470e – InsaneCat Dec 09 '17 at 05:31
  • 1
    Yes, none of those really helped – JoseRivas1998 Dec 09 '17 at 05:36
  • 3
    as in the link provided above by @InsaneCat: "background drawables must be opaque whilst foregrounds can contain transparency." Unfortunately this is no where clear in the documentation. – Nonos Dec 19 '17 at 19:43

5 Answers5

17

TL;DR Adaptive Icons cannot have transparent Background

Transparent background will be opaque black.

According to the source code of Android 8.0 framework, the <background> layer is inherently opaque; the framework fills the background with opaque black, as mentioned in the question.

The framework has a class AdaptiveIconDrawable, which draws adaptive launcher icons. https://developer.android.com/reference/android/graphics/drawable/AdaptiveIconDrawable.html
The element <adaptive-icon> creates an instance of it:

This class can also be created via XML inflation using <adaptive-icon> tag in addition to dynamic creation.

The source code of method draw in AdaptiveIconDrawable.java is:

@Override
public void draw(Canvas canvas) {
    if (mLayersBitmap == null) {
        return;
    }
    if (mLayersShader == null) {
        mCanvas.setBitmap(mLayersBitmap);
        mCanvas.drawColor(Color.BLACK);
        for (int i = 0; i < mLayerState.N_CHILDREN; i++) {
            if (mLayerState.mChildren[i] == null) {
                continue;
            }
            final Drawable dr = mLayerState.mChildren[i].mDrawable;
            if (dr != null) {
                dr.draw(mCanvas);
            }
        }
        mLayersShader = new BitmapShader(mLayersBitmap, TileMode.CLAMP, TileMode.CLAMP);
        mPaint.setShader(mLayersShader);
    }
    if (mMaskBitmap != null) {
        Rect bounds = getBounds();
        canvas.drawBitmap(mMaskBitmap, bounds.left, bounds.top, mPaint);
    }
}

In short, a Canvas instance receives a bitmap to draw into, and initially it fills the bitmap with black color. Then the foreground drawable and the background drawable do the job:

mCanvas.setBitmap(mLayersBitmap); // reset
mCanvas.drawColor(Color.BLACK);   // fill
for (int i = 0; i < mLayerState.N_CHILDREN; i++) { // two layers
    ...
    final Drawable dr = mLayerState.mChildren[i].mDrawable;
    ...
        dr.draw(mCanvas); // draw
    ...
}

The Color.BLACK is opaque:

0xff000000

The method drawColor fills the bitmap with it, using SRC_OVER mode:

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.

So, the background will be always opaque, even if you set transparent color to the background, as in the screenshot below (from my answer to a similar question).

NoAdaptive and Adaptive

Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
qtmfld
  • 2,916
  • 2
  • 21
  • 36
  • how to make it white? – Dika Apr 19 '18 at 07:41
  • 1
    It will be white, if you set `#ffffffff` in "res/values/color.xml" for `@color/transparent`, as in the question. – qtmfld Apr 19 '18 at 07:51
  • 1
    @Dika Some launcher apps such as [Pixel Launcher](https://play.google.com/store/apps/details?id=com.google.android.apps.nexuslauncher) will change the background color of launcher icons to white, as I wrote in "Update #2" section in [my answer to a similar question](https://stackoverflow.com/a/49667272/9298629). – qtmfld Apr 19 '18 at 08:01
  • Why Android is trying to imitate iOS by removing the good features of itself?! – doctorram May 15 '21 at 18:30
10

Easiest Solution ever:

  1. Assuming you have a PNG file with a transparent background (preferably, square ratio), let's call it "app_icon.png", put it in your drawable folder P.S: It doesn't matter how large it is, what matters is the square ratio.

  2. Go to AndroidManifest.xml, in your main application tag, check for the

    android:icon="@mipmap/app_icon"

and change it to

android:icon="@drawable/app_icon"
  1. Delete all your "mipmap" folders now since you don't need them.

Why does this work: This tells android to use a drawable DIRECTLY from the folder as an icon, without going through the hurdle of mipmap XML files.

Congratulations, now you will have a logo with a transparent background.

yuroyami
  • 814
  • 6
  • 24
Arya Danesh
  • 138
  • 1
  • 7
1

You said a legacy icon is "useless", but the only logical solution is to use a legacy icon. If you don't create an adaptive icon at all, Android 8.0 and above will have to use a legacy icon. Moreover, an adaptive icon with a transparent background is a contradictory concept because the main feature of an adaptive icon is that the foreground is contained within a shape determined by the OEM. But if the background is transparent, how can there be any shape? You might as well be asking how to square a circle.

0

Generate your launcher icons using Android Studio with a transparent XML background layer, then delete ic_launcher.xml and ic_launcher_round.xml from res/mipmap-anydpi-v26 (the ones with the black background). Android will then have to fallback on one of the other versions (hdpi, etc.) with a transparent background.

Pat Lee
  • 1,567
  • 1
  • 9
  • 13
-5

just play with 8 digit hex code example : 80DCDCDC im using this for a long time for transparency

WHAT IF I
  • 1
  • 2