concept drawable in category android

This is an excerpt from Manning's book Android in Practice.
After the TextView, we next have an ImageView
. For it, we’re specifying the src attribute as @drawable/droid, another external resource reference, this time to a drawable named droid
. We’ll discuss drawables in chapter 4. For now, we need to understand that we’ve included a droid.gif image file in the res/drawable-mdpi directory of the project and that way Android can find and use it (this file is available with the code download for the book; we initially grabbed it from the Android goodies page: http://www.android.com/media/goodies.html). With our layout out of the way, let’s take a closer look at how the resource references work.
Drawables always live in the res/drawables folder and its various configuration-specific variants and come in two different formats: binary image files and XML. If you want to use a custom image file in your app, it’s as easy as dropping it into that folder. The ADT will discover the drawable and generate an ID for it that’s accessible in Java through R.drawable. the_file_name, (like with any other Android resource). The same is true for XML drawables, but you’ll have to write these first, and we’ll show you in a minute how to do that.
If you’ve placed a drawable in the drawables folder, you can access it from an Activity by a call to getResources().getDrawable(id). But it’s more likely that you’ll use drawables in your style or view definitions, for use as backgrounds or other graphical parts of a widget, and sometimes it’s difficult to identify them as such. Let’s recall our list style definition from listing 4.9:
<style name="MyMoviesListView" parent="@android:style/Widget.ListView"> <item name="android:background">#A000</item> <item name="android:fastScrollEnabled">true</item> <item name="android:footerDividersEnabled">false</item> </style>We’re already using a drawable here. Can you see it? To be fair, it’s not jumping out. It’s the color we used as the list’s background. For some attributes, Android allows color values where it usually expects a normal drawable, such as a bitmap image. In that case, it’ll turn the color into a ColorDrawable internally. Note that for some reason this doesn’t work everywhere. For instance, the android:windowBackground attribute doesn’t accept color values.
We can also define special drawables entirely in XML. When doing so, the root element is typically the drawable’s class name with the Drawable part stripped off. For instance, to define a ColorDrawable you’d use the <color> tag, as we showed you in the previous section (we’ll see two exceptions to this rule in a moment, admittedly making this a little confusing). Note that not all kinds of drawables can be used everywhere; list selectors for instance don’t accept a plain color value because a selector has more than one state and one color drawable isn’t enough to reflect that. Curiously, a plain image would work here, though.
What we’ve done here is replace the root element of the list selector from a shape to a shape switcher—a selector. Selector drawables are defined in terms of <item> elements, each of which takes two arguments: a state, and a drawable to be displayed whenever the view to which this selector is being applied enters that state (you can also use color values as we’ll see in a minute). Figure 4.11 shows the selector in both states.
Figure 4.11. The new list selector in the default (left) and pressed (right) states. Note how it changes colors when in the pressed state.
![]()
You can switch all sorts of drawables using the <selector> tag, not just shape drawables. The most common examples are nine-patch images, which we’ll cover next. In fact, Android uses this combination of selectors and nine-patches all over the place to render its system UI.
A drawable in Android, defined by the Drawable class, is a graphical element displayed on the screen, but unlike a widget is typically noninteractive (apart from picture drawables, which actually allow you to record images by drawing onto a surface).
Apart from images and colors, there are drawables for custom shapes and lines, drawables that change based on their state, and drawables that are animated.
Drawables are worth covering separately, because they’re powerful and ubiquitous in Android. You need them practically everywhere: as backgrounds, widget faces, custom surfaces, and generally anything involving 2D graphics. We won’t cover every kind of drawable here, only the most widely used ones. Additionally we’ll come back to drawables in chapter 12, where we’ll discuss 2D/3D graphics rendering. For now, we’ll focus on drawables that are important for styling your applications.
. For it, we’re specifying the src attribute as @drawable/droid, another external resource reference, this time to a drawable named droid
. We’ll discuss drawables in