Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 240
Rajan Kadeval Avatar asked Dec 01 '15 08:12

Rajan Kadeval


3 Answers

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:

  • One by CommonsWare
  • Another by ianhanniballake
like image 91
Farbod Salamat-Zadeh Avatar answered Oct 30 '22 03:10

Farbod Salamat-Zadeh


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.

like image 26
Yoann Hercouet Avatar answered Oct 30 '22 03:10

Yoann Hercouet


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.

like image 38
Tafel Avatar answered Oct 30 '22 02:10

Tafel