I. Android tools attributes
- Error handling attributes
- Design-time view attributes (Don’t miss this!)
- Resource shrinking attributes
II. Sample Data (Best design helper!)
I. Tools Attributes
tools:shrinkMode
tools:keep
and tools:discard
Android Tools Attributes and Sample Data — Must know helpers!
Every year at I/O (Google’s biggest developer conference event), Google reveals some new capabilities with extensive quality features to make our lives easier and in turn build better user-friendly products. This year, I/O 18, Google introduced some new Android Studio Tool features to help us get a better grip of design at compile time. In this article, I will go through two of such tool features.
I. Android tools attributes
- Error handling attributes
- Design-time view attributes (Don’t miss this!)
- Resource shrinking attributes
II. Sample Data (Best design helper!)
I. Tools Attributes
Tools Attributes are the XML attributes defined in the tools
namespace which provide compile-time behaviors and design time features. Most importantly, Android build tools remove these attributes when it builds an APK. Hence, these tools are very useful and don’t affect the APK size. Many of these attributes were already present. Recent Google I/O majorly came up with Design-time view attributes. Let’s look at a few attributes from each category used by all developers across the industry:
the lint to ignore the error.
There are the attributes which help you preview layouts in your editor which many a time gets linked at runtime only. They define layout characteristics that are visible on in Android Studio Layout Editor.
tools:showIn
Consider a case when you design a custom toolbar layout. In the preview, you only see the layout of your custom toolbar along with its characteristics.
Problem is that we don’t know how this toolbar will look with our actual content of the activity. Whether the colors, size and other characteristics will match or not. Here comes the tools:showIn
attribute, which lets you see the preview of the toolbar layout along with the content which you choose to see here.
tools:listItem
Whenever we include a ListView/RecyclerView, its preview is just plain text. We all know how inefficient it can be to run the application every time to see the actual item on the device.
Woah! Did you see the magic? You actually bind the item layout to the recycler view and you can see how items will look like a list in the preview only. You must remember that it’s only a preview! This tag will not affect any runtime behavior and not even the size of the APK.
You can also see how your list will look with a certain number of items using tools:itemCount
Resource Shrinking Attributes
1. tools:shrinkMode
This attribute allows you to specify whether the build tools should use “safe mode” (play it safe and keep all resources that are explicitly cited and that might be referenced dynamically with a call to
Resources.getIdentifier()
) or “strict mode” (keep only the resources that are explicitly cited in code or in other resources).
Default is shrinkMode=”safe”
and for strict mode, we use shrinkMode=”strict”.
2. tools:keep
and tools:discard
When you use resource shrinking to remove unused code, these attribute helps you to customize which resource to shrink. To use, create an XML file in your resources directory (for example, at res/raw/keep.xml
) with a <resources>
tag and specify each resource to keep in the tools:keep
attribute and each resource to discard in the tools:discard
attribute as a comma-separated list.
What’s the point of explicitly discarding a resource if we know that we are not gonna use it? It’s helpful when you are using different build variants! Sometimes shrinker doesn’t shrink a resource because it appears to be used somewhere. However, we know that this resource is not getting used into a certain build variant. We can explicitly create a res/raw/keep.xml
to discard that resource so that we have optimized APKs.
For a better understanding on Shrinking your resources you may check this.
II. Sample Data
This attribute allows you to specify placeholder text and images at preview time itself.
@tools:sample/first_names
Common first names.@tools:sample/date/day_of_week
Randomized dates and times for the specified format.@tools:sample/date/ddmmyy
@tools:sample/date/mmddyy
@tools:sample/date/hhmm
@tools:sample/date/hhmmss
@tools:sample/avatars
Vector drawables that you can use as profile avatars.