47

Creating a Drawable that is completely empty seems like a common need, as a place holder, initial state, etc., but there doesn't seem to be a good way to do this... at least in XML. Several places refer to the system resource @android:drawable/empty but as far as I can tell (i.e., it's not in the reference docs, and aapt chokes saying that it can't find the resource) this doesn't exist.

Is there a general way of referencing an empty Drawable, or do you end up creating a fake empty PNG for each project?

Kai
  • 5,260
  • 5
  • 29
  • 36

9 Answers9

102

For me it didn't work when I tried to use @android:id/empty when making an empty drawable. For me, @android:color/transparent was the one.

Pi Delport
  • 10,356
  • 3
  • 36
  • 50
Emil
  • 1,414
  • 1
  • 12
  • 14
14

I use an empty ShapeDrawable, i.e. create my own drawable/empty.xml containing just <shape/>

JulianSymes
  • 2,135
  • 22
  • 24
11
<shape />

creates an empty shape and does the trick for me. Or create a file empty_drawable.xml containing:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" />

so you can use it elsewhere.

You can also make a shape disappear when the view it is used in is not enabled by creating my_disablable_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <shape android:shape="oval">
            <size android:height="10dp" android:width="10dp" />
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape />
    </item>
</selector> 
Hans
  • 1,886
  • 24
  • 18
6

Use @android:color/transparent and don't forgot add android:constantSize="true" on <selector>

Andrey Timofeev
  • 106
  • 1
  • 3
5

@null in XML has the same effect as using a null as a Drawable in Java.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I wish this would work, but if I use it in drawable XML and that's set as a background in a layout XML, I get error " tag requires a 'drawable' attribute or child tag defining a drawable" – androidguy Dec 02 '16 at 00:59
5

For me, using @android:drawable/empty would show an error and prevent compiling. Using @android:id/empty fixed the error and let me compile, but showed "NullPointerException: null" in the Eclipse Layout editor. I changed it to @android:color/transparent and now everything is fine.

I can't vote up yet or I would have up'ed Emil's answer.

wirbly
  • 2,183
  • 1
  • 24
  • 24
2

Kai, I don't know why this is the case, but I've gotten the same issue. I think it might be related to the version of Android you're compiling for? In any case, I found that simply using @android:id/empty where you would use @android:drawable/empty does the trick.

2

To create an empty Drawable image, you may use ShapeDrawable with transparent color:

val shapeDrawable = ShapeDrawable(OvalShape())
shapeDrawable.paint.color = context.getColor(android.R.color.transparent)

If the image size is important, use:

shapeDrawable.intrinsicWidth = 100
shapeDrawable.intrinsicHeight = 100
Benny
  • 2,233
  • 1
  • 22
  • 27
2

I had the same problem. My App crashed with an empty vector image. I solved it by adding a transparent/invisible path element. I tested it with API 22 and 30.

ic_empty.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:width="24dp"
   android:height="24dp"
   android:viewportWidth="24"
   android:viewportHeight="24">
       <path android:strokeAlpha="0" android:pathData="M0,12h24"/>
</vector>
Heiko Zelt
  • 21
  • 3