My name is Kelly Shuster, and I work at a company called Ibotta; on their Android team.
This talk is on the Android Support Library. The Android Support Libraries let you bridge and backport the current, latest operating system feature set back to older operating systems on older devices.
Much time that we spend with Android development is spent looking forward, and anxiously looking at the next thing, but I think that it’s fun to take a look back and see how far we’ve come.
A Quick Story
In 2011, the first Support Libraries were released. They were called the v4 and the v13 Support Library, It was exciting to have the ability to backport some features to your older operating systems.
In 2012, while Google was still releasing updates to their Support Libraries, a big library was released, not by Google, called
ActionBarSherlock. Google did not support it in their Support Library. This was third party library written by Jake Wharton that allowed you to backport the action bar to your older operating systems.
In 2013, we obtained the v7
appcompat library that gave you support for the action bar, and people no longer needed the third party dependency.
In 2014, as they continued to update the Support Libraries they also released new ones that provided support for CardView, RecyclerView, the Palette library, the Leanback library, and the Wear library.
In 2015, they released annotations, Design Support Library, Custom Tabs, Percent, the App Recommendation Support, and Preference Support Libraries for various versions.
In 2016, they released a Vector Drawable Support Library, split up the v4 library into different components, and released the
exifInterface Support Library.
Get more development news like this
When people talk about the Android Support Library, sometimes they say it singularly, but there are many Support Libraries, and it’s a good thing that they’ve broken these out into different components because if you had to conclude one library that would be a lot of code that you’re pulling into your app that you don’t need.
A Few General Tips
The naming is weird and confusing.
appcompat-v7:25.1.0 references the API version that your Support Library has been built against. If you’re targeting an API 25, you want to pull in the Support Libraries that are version 25. (+ the next two numbers, which reference the actual version of the Support Library itself).
In the beginning, v7 used to mean that everything in this Support Library can be used all the way back to API 7; any method you call will work for operating systems all the way down to API 7. But that’s no longer the case because as Android has progressed, more people are running on modern versions.
Google has slowly been increasing the level of APIs that these libraries support. You cannot rely on the name of the library to know how far back it supports, and you have to check the documentation.
You have to use a Support Library version that matches with your target API. Here in this example, I’m getting an error in my build file because I’m trying to use the 25 version libraries, but I’m targeting API 26.
I like to run this dependencies script:
gradle -q dependencies app:dependencies, where the app is the name of your app module. This will give you a run down of all the dependencies.
If you’re using the 25.+ versioning system, this command will tell you the actual version, and figure out where the mismatch is occurring.
Beware of Stubbed Methods!
When you pull in the Support Library that supports back to a certain version of the operating system, you may assume that you can call a method that existed then, but not all of the methods work. It’s important to check on older APIs so ensure that the app is doing what it’s supposed to be doing.
For example, ViewCompat.setElevation on a TextView. I’m calling a ViewCompat method to set the elevation, and I’m assuming that it would work, but unfortunately, this doesn’t work on pre-Lollipop devices. There’s no lint warning, and there’s no compile error.
When you’re using the Support Library, always make sure that you’re testing on the lowest operating system that you want to support to make sure that you’re not merely calling stubbed methods that are doing nothing.
A famous one is ViewPager. You get this in the v7 library, but it’s defined in the v4 library (the v7 library includes the v4 library).
The ViewPager is this component that allows you to have a big horizontal list you can swipe back and forth. When you pull it in, it denotes the v4 Support Library. You can pull it into your layout where you want it to exist, and you’ll create a
pagerAdapter, which is what links your data to this ViewPager element.
I’ve defined my custom pager adapter, and then you link up your adapter to the ViewPager by set, ViewPager.setAdapter.
The TabLayout is in the Design library. Several years ago, Google released sample code for implementing this, and then every few months they’d update the sample code. Thus, every time I started a new project, I had to go in and delete the old files and pull in the new files.
Now they’ve released it as part of their Design Support Library. I pop in the tab layout widget in my XML file where I want to use it, and then in my custom pager adapter from the ViewPager, I can set the page title to whatever I want.
The Toolbar is an
appcompat-v7. The ActionBar used to be for a title and an icon along with an overflow menu, but now the Toolbar gives you freedom of what you want to put up there.
Here’s an example from our app (see slides). First, set the theme for your app to NoActionBar, because you don’t want it to default. Then, define your Toolbar in your layout file. A Toolbar is like a layout, and then you can put anything you want inside of the Toolbar layout. I an ImageView and a TextView.
Then, you call setActionBar or setSupportActionBar with the Toolbar.
The FloatingActionButton and the Snackbar
The FloatingActionButton and the Snackbar are two pieces from the Design Support Library. They act well together, and they’re core pieces of the material design language.
For example, you click the button in the lower right hand corner and then the Snackbar pops up. The FloatingActionButton is like a regular button that’s circular and has an elevation set on it.
It doesn’t automatically go in the bottom right corner of your screen, and you will need to set that on your own. You set the view like you normally do, and set an OnClickListener like a regular button.
The FloatingActionButton moves up with the Snackbar. That is something you can get by default if you use the CoordinatorLayout, and that’s the recommended way to do this. Make sure that your FloatingActionButton is a direct descendant of a CoordinatorLayout, and then this will happen automatically for you.
The TextInputLayout in the Design library allows you to have a hint in your EditText box; it swoops up to become the title when you type. It also gives you awesome styling capabilities for error messages.
To style the error color that comes up, you have to style the error text appearance as a theme on the TextInputLayout, then you can set the regular EditText style in your TextInputEditText.
PercentRelativeLayout is in the Percent Support Library, and it allows you to apply percentages when working with RelativeLayout instead of only just the weight. This is much like how it’s done in LinearLayout.
Make sure you don’t define width and height like normal. Sometimes you get an error in Android Studio, but it will still compile.
ConstraintLayout is the new powerful layout tool where you are able to place items relative to each other.
Vector Drawable Library
The Support Vector Drawable Library allows vectors and graphic support for older operating systems.
There is a format called SVG (Scalable Vector Graphic) that allows for crisp images. An SVG is a set of instructions for how to draw an image. Thus, an image is not degraded because it’s redrawn every time it’s resized.
Android Studio, or Android, supports Vector Drawable, which is their own version of an SVG, and it’s important to know that they’re not a one to one relation.
The Vector Drawable supports a subset of the commands of SVG. To get an SVG imported into Vector Drawable, right click on Drawable -> New Vector Asset, and go through Android Studio wizard. It will convert your SVG into the Vector Drawable.
If you want to use this in your library, or in your app, first import the library, then set
vectorDrawables.useSupportLibrary to true. In ImageView, pull in an app:srcCompat drawable, but do not pull in the Android:src like usual ImageViews.
25.0.0 To 25.3.1
From 25.0.0 to the most recent one, 25.3.1, these are the Support Libraries that can work with the most recent released operating system, API 25.
ArraySet is available as of 25.1.0. It’s more efficient than a traditional HashSet, and it’s similar to ArrayMap, but it contains only one item per entry, where ArrayMap contains two items per entry. This is much like a combination of a HashSet and ArrayMap.
It’s slower than HashSet, so it’s not for large items.
The BottomNavigationView is a new design pattern in Android. Phones are getting so big that it’s hard to one-handed get the navigation menu and slide it over, but if you have a bottom navigation menu, it’s easier with your thumb to click on items.
We recently updated the UI of our app to include the BottomNavigationView, and I was impressed at the depth of the guideline documentations to explain how this BottomNavigation should work.
The Support Libraries, starting with 26.0 releases, are only going to support back to API 14, Ice Cream Sandwich.
You used to have to download the Support Libraries through Android Studio, now you can pull them in like any other library.
Before, when you wanted to support fonts you have to put the font in an assets folder, then create a special typeface that referenced it. Then, every TextView had to be special to handle the font.
Now, fonts work as they should. Drop your TTF file into a res/font directory. You can open up the TTF file, and it gives you a window of what font looks like.
Before, if you made your TextView layout big, your text size would always be the same no matter what you did. But now there is Autoscaling TextView.
First set the max and min size of how big and small you want to allow your font to get. The scale type should be uniform, because you want it to stretch perfectly as your layout stretches. I’ve set the layout heights for these two different TextViews, one is at 100dp and the other at 10dp.
My favorite part of API 26 Support Library is
findViewById. All instances of findViewById method now return a View type which extends View - we do not have to cast every call anymore.
About the content
This talk was delivered live in July 2017 at 360 AnDev. The video was recorded, produced, and transcribed by Realm, and is published here with the permission of the conference organizers.