Monday, February 13, 2017

Vector Asset Studio and API level below 21

One way to add vector (i.e. slick regardless of the device screen resolution) Material icons to an Android application is to use built-in Vector Asset Studio.

The documentation mentions that if your minSdkVersion in build.gradle is below 21, the build will generate PNG for your supported screen resolutions (which may not be all possible resolutions in the wild, and also may increase the size of your APK). If you don’t want to have PNG you can add vectorDrawables.useSupportLibrary = true, which conveniently adds support for rendering vector graphics inside ImageView (and ImageButton etc).

Note that contrary to a popular myth, it is not necessary to update all layout xml files to use AppCompatImageView: only if in your Java code you inherit your own class from an ImageView (e.g. to extend its functionality), you should change it. But that doesn’t mean that you have to chase after each and every library: if a library isn’t used to display vector graphic (such as using de.hdodenhof:circleimageview to display logged in user’s photo) you don’t have to change it.

However, there’s still a small catch: if you forget to change android:src property name to app:srcCompat in at least one of your layouts, the application will crash (trying to expand the layout) and it may be very compilcated to find the reason. Watch out for new images or buttons that you add later (especially FloatingActionButton) as well.

1 comment:

  1. This comment has been removed by a blog administrator.