Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 20  Data Binding and MVVM


Now wire up SoundAdapter in onCreateView(...).


Listing 20.11  Wiring up SoundAdapter (BeatBoxFragment.java)


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FragmentBeatBoxBinding binding = DataBindingUtil
.inflate(inflater, R.layout.fragment_beat_box, container, false);


binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
binding.recyclerView.setAdapter(new SoundAdapter());


return binding.getRoot();
}


Importing Assets


Now to add the sound files to your project and read them in at runtime. Rather than use the resources
system for this job, you will use raw assets. You can think of assets as stripped down resources: They
are packaged into your APK like resources, but without any of the configuration system tooling that
goes on top of resources.


In some ways, that is good. Because there is no configuration system, you can name assets whatever
you want and organize them with your own folder structure. In other ways, though, it is bad. Without
a configuration system, you cannot automatically respond to changes in pixel density, language, or
orientation, nor can you automatically use the assets in layout files or other resources.


Usually resources are the better deal. However, in cases where you only access files programmatically,
assets can come out ahead. Most games use assets for graphics and sound, for example – and so will
BeatBox.


Your first step will be to import your assets. Create an assets folder inside your project by right-
clicking on your app module and selecting New → Folder → Assets Folder. Leave the Change Folder
Location checkbox unchecked, and leave the Target Source Set set to main (Figure 20.3).

Free download pdf