Can we use VectorDrawable or VectorXML as icons for push notifications in android?

11,811

Solution 1

Can we use VectorDrawable or VectorXML as icons for push notifications?

Yes, just call the vector drawable the standard way for notifications:

.setSmallIcon(R.drawable.my_vector)

In order to use the transparency (notification icons are only white and/or transparent), you will have to use the alpha channels when settings the colors in the vector XML, meaning #00000000 for transparent and #FFFFFFFF for white.

Solution 2

VectorDrawables will only work as notification icons for versions higher than (or equal to) Android Lollipop - i.e. API 21.

I know this because I did try using .setSmallIcon(R.drawable.my_vector) as shown in one of the other answers here, and although this works perfectly fine for API 21 and above, I got the following error for versions prior to Lollipop:

android.app.RemoteServiceException: Bad notification posted from package com.example.app: Couldn't create icon: StatusBarIcon(pkg=com.example.appuser=0 id=0x7f02005a level=0 visible=true num=0 )

There are also other answers on Stack Overflow supporting this argument:

Solution 3

UPDATE 2020

Yes, it is definitely possible. But let Android Studio take care of icon creation. Otherwise you will be at risk of not supporting older Android versions (check other answers).

So how to create the right files with Android Studio:

  1. Right click on a file on the left side of Android studio
  2. New > Image Assets enter image description here
  3. Icon Type > Notification Icons

enter image description here

  1. Select a vector image (.svg for example)

Android studio will create all the correct files needed.

Solution 4

For version < 21,

If you want to directly pass in vector drawable resource id into setSmallIcon(): No way.

For setLargeIcon() indirectly, yes. Use

VectorDrawableCompat drawable = VectorDrawableCompat.create(context.getResources(), resource id, theme);

then create Bitmap from this drawable and pass into setLargeIcon()

Solution 5

If you insist to use Vector drawable try converting it to bitmap :

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_vector_drawable);
                    mBuilder = new NotificationCompat.Builder(context)
                            .setLargeIcon(bitmap)
                            .setOngoing(!cancelable);

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        mBuilder.setSmallIcon(getNotificationIcon());
                    }
Share:
11,811

Related videos on Youtube

Rajan Kadeval
Author by

Rajan Kadeval

A passionate developer brewed in a fast paced startup environment, experienced in building web/mobile applications starting from scratch to taking it live, managing client, testing, technical support,building APIs, agile development, TDD, scrum master, design &amp; architecture, project management. I believe that the technologies/methods are less important, but the thrill of people finding value in my creation is what keeps me moving

Updated on June 25, 2022

Comments

  • Rajan Kadeval
    Rajan Kadeval about 2 years

    I am using PNG image but its size is getting too big so I have to compromise with its quality. So i was thinking vectors may be the another way around ? Example will be a great help.

    • Madhur
      Madhur over 8 years
      if your PNG image is big include it in drawable-xxhdpi resourse folder and then use it!
    • Mohamad Damba Putrabangga
      Mohamad Damba Putrabangga over 8 years
      So the problem is your png image size to big for icon? you can just resize for different screen. You can use romannurik.github.io/AndroidAssetStudio. It's help you to create icon for different sizes and densities.
    • Rajan Kadeval
      Rajan Kadeval over 8 years
      I have re-sized and included it in drawable-xxhdpi and was using till now but because of lollipop update i had to create transparent image and use it with background color which deteriorated its quality even further. so is there anyway i can create colored drawablevector and use it as icon?
    • John
      John about 8 years
      Is it for "setSmallIcon(int)", "setLargeIcon(Bitmap)" or "NotificationCompat.BigPictureStyle.bigPicture(Bitmap)" ? It is posible to raster a VectorDrawable to Bitmap, using the DrawingCache. ... but it would bring you back to the size issue, just moved from build time to run time. It would still lower the APK size, if this is your goal. You can use actual VectorDrawable for the "setSmallIcon(int)" but this will only work on Lollipop devices or higher (I tested it on Galaxy S4), missing - currently - about 60% of the market : developer.android.com/about/dashboards/index.html
  • Farbod Salamat-Zadeh
    Farbod Salamat-Zadeh almost 8 years
    I think this only works for API 21+ as I used the above, but got an error like this for pre-Lollipop devices. Also, see this.
  • Yoann Hercouet
    Yoann Hercouet almost 8 years
    @FarbodSalamat-Zadeh Yes you need to check the user's SDK with a condition (if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP))