Chapter 20 Data Binding and MVVM
public class BeatBoxBindingAdapter {
@BindingAdapter("app:soundName")
public static void bindAssetSound(Button button, String assetFileName) {
}
}
Simply create a static method in any class in your project and annotate it with @BindingAdapter,
passing in the name of the attribute you want to bind as a parameter. (Yes, this really works.) Whenever
data binding needs to apply that attribute, it will call your static method.
You will probably think of one or two operations you would like to use data binding with on the
standard library widgets. Many common operations already have binding adapters defined – for
example, TextViewBindingAdapter provides additional attributes for TextView. You can read these
binding adapters yourself by viewing the source in Android Studio. So, before you write your own
solution, type Command+Shift+O (Ctrl+Shift+O) to search for a class, open up the associated binding
adapters file, and check to see whether it already exists.
For the More Curious: Why Assets, Not Resources
Technically, you could have used resources in this chapter instead of assets. Resources can store
sounds. Stash a file like 79_long_scream.wav in res/raw, and you can get at it with the ID
R.raw.79_long_scream. With sounds stored as resources, you can do all the usual resource things, like
having different sounds for different orientations, languages, versions of Android, and so on.
So why did we choose assets? The biggest reason is that BeatBox has a lot of sounds: more than 20
different files. Dealing with them all one by one in the resources system would have been cumbersome.
Resources do not let you ship out all your sounds in one folder, nor do they allow you to give your
resources anything other than a totally flat structure.
This is exactly what the assets system is great for. Assets are like a little file system that ships with
your packaged application. With assets, you can use whatever folder structure you want.
So with assets, if you want to add a new sound file, all you have to do is add it to your sample_sounds
folder. Since they give you this kind of organizational ability, assets are commonly used for loading
graphics and sound in applications that have a lot of those things, like games.
For the More Curious: Non-Assets?
AssetManager has methods called openNonAssetFd(...). You might reasonably ask why a class
dedicated to assets would concern itself with non-assets. We might reasonably answer, “These
aren’t the droids you’re looking for,” so that you might go on believing that you never heard of
openNonAssetFd(...) in the first place. There is no reason that we know of to ever use this method, so
there is no real reason to learn about it. You did buy our book, though. So we might as well throw this
answer out there for fun:
Earlier we said that Android has two systems, assets and resources. The resources system has a nice
lookup system, but some resources are too big to fit inside that system. So these big resources – images
and raw sound files, usually – are actually stored in the assets system. Under the hood, Android opens
these things itself using the openNonAsset methods, not all of which are publicly available.
When would you need to use these? As far as we know, never. And now you know, too.