Tutorial details

Android Drawables - Tutorial | Android Tutorial

How to use Drawables in Android

Description

Comments (0)

Overview PAGE TOP

This tutorial describes the usage of Drawables in Android.

1. Drawables PAGE TOP

A Drawable resource is a general concept for a graphic which can be drawn. The simplest case is a graphical file, which would be represented in Android via a BitmapDrawable class. Bitmaps are typically stored in one of the res/drawable folders. The Android project creation wizard creates several of these folders by default, you can provide different sized files for different resolutions of Android devices. If you only provide one file for all sizes the Android system will scale the resource.

In addition to graphical files, Android supports XML drawables and 9-patch graphics. XML drawables are used to describe shapes (color, border, gradient), State and Transitions and more.

9-patch graphics are used to define which part of a graphic should be stretched if the View which uses this graphic is larger than the graphic.

2. Accessing drawables PAGE TOP

Graphical assets which are stored in the folders starting with drawable are managed by the Android. They can get directly accessed in XML via @drawable/filename whereby filename filename is the filename without extension. For example to access the hello.png file, you would use @drawable/hello

In code you can also access these drawables. Most views accept an resource ID as input parameter. For example the following code shows how to set a drawables as background to an ImageView.

3. Bitmaps and drawables PAGE TOP

Android allows you to use the Bitmap class for working with bitmaps. The Drawable class is more general, hence you frequently want to convert your bitmaps into drawables.

All bitmaps which are stored in the drawable folder can be directly used as a Drawable objects.

You can also load Bitmaps manually and convert them into Drawables objects. To load a bitmap from your assets folder or another folder you must create the Bitmap yourself. The following example code shows this for the assets folder.

4.1. Shape Drawables PAGE TOP

Shape Drawables are XML files which allow to define a geometric object with colors, borders and gradients which can get assigned to Views. The advantage of using XML Shape Drawables is that they automatically adjust to the correct size.

Confirm vote

Please check your content

It appears as though you entered a website, phone number, or email address.
Please, do not exchange your Skype/email contact on the platform, for any questions regarding the product do
not hesitate to make your demand here, your question could be useful for future buyers on the platform.
Moreover, writing on the comments area allows us to have a record of the issues and to act in case of
any problem.