The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
drawable resources. Except when they are
And except when they are multiple resources, combined together at runtime
to create an image with a “squircle” background. And…
Are you confused yet?
In theory, setting up icons for your app would be quite simple, and for years that was the case. Nowadays, setting up an app icon is unnecessarily complex, though at least Android Studio has an Asset Studio tool to try to make it a bit simpler.
In this chapter, we will explore what it takes to set up one of these app icons.
Each app has an icon. This is used for places like the list of installed apps in Settings. Traditionally — and to reduce user confusion — this icon is also used for the home screen launcher.
Google has been making this icon increasingly complicated over the past few years:
mipmapresources, while everything else is a
Most other icons — other than notification icons – are comparatively straightforward.
If you right-click over pretty much any directory in the Android Studio tree, the context menu will have an “Image Asset” option. This is also available from File > New > Image Asset. This brings up the Asset Studio, to help you to assemble icons.
By default, the Asset Studio has its “Icon Type” drop-down set for “Launcher Icons (Adaptive and Legacy)”, which is how you set up an app icon nowadays:
Figure 52: Asset Studio, As Initially Launched
The overall name for your launcher icon is found in the Name field, above
the tabs. The default is
ic_launcher, and unless you have a good reason
to change it, you are best served by leaving it alone.
What you would ordinarily think of as your icon is what Android Studio and Android 8.0+ refer to as the “Foreground Layer”. The Asset Studio starts on the Foreground Layer tab for you to configure this layer.
Your icon can come from three main sources:
Figure 53: Asset Studio, Showing Clip Art Options
Figure 54: Asset Studio, Showing Text Options
For all three of these, you can:
That latter choice is particularly important, as you need to keep your foreground layer content within the “safe zone”. That shows up in the previews as a dark gray circle. So long as your foreground layer is in the safe zone, you should not have to worry about any launcher accidentally cutting off part of the layer when it renders your app icon.
In the preview area, by default, you will see a blue-green grid behind your foreground layer. That is the default background layer. If you would like to use something else, click the “Background Layer” tab:
Figure 55: Asset Studio, Showing Background Layer
Your two main options are:
The background needs to be designed to be cropped into a variety of shapes, such as those shown in the preview (circle, various rounded forms of squares, etc.). Hence, outside of flat colors, typical backgrounds will be gradients or simple patterns, such as the default grid.
On Android 8.0+ devices, the foreground layer, background layer, and launcher-chosen shape (e.g., squircle) combine to create your app icon.
On older devices, the app icon is whatever you choose it to be, where the Legacy tab helps you decide what that is:
Figure 56: Asset Studio, Showing Legacy Options
The most important legacy option is the “Legacy Icon (API <= 25)” section.
minSdkVersion is below 26, this will be the icon rendition
that will be used as your app icon by default. The Asset Studio will merge
your foreground and background layers itself, then apply your selected
shape from the drop-down (e.g., square).
For Android 7.1 devices, you can also opt to have the Asset Studio create a separate round icon, that you can declare in your manifest, as will be seen later in this chapter.
If you are going to be distributing your app through the Play Store, you can also generate a Play Store rendition of your icon. This is reminiscent of the legacy icon, but at a higher resolution.
Once you have adjusted your app icon via the three tabs, click Next at the bottom of the Asset Studio wizard. This will bring up the final wizard page, showing you what will be generated for you by the wizard:
The Output Directories tree shows you each file that will be created or replaced. Those that show up in red are ones that will be replaced; ones that show up in black are new files. Typically, unless you changed the icon or layer names, most of the files will be replacements for files that exist already.
Some of these files will be typical bitmap-style resources. Some are vector drawables.
Then, in your manifest, you can have
android:icon and optionally
android:roundIcon attributes on the
<application> element to associate
your icons with your app. If you did not change the names of the icons,
then your manifest should already have the appropriate attribute values.
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.commonsware.myapplication" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
The preview of this section was traded for a bag of magic beans.