Creating an empty Drawable in Android

Viewed 38690

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?

9 Answers

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

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>

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
Related