How to create Drawable from resource
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android, a Drawable is the runtime object you use to display an image, shape, vector, or solid color. If the source lives in res/drawable, the usual job is not “creating” the drawable from scratch, but loading the correct resource safely for the current theme and API level.
Prefer ContextCompat.getDrawable
The standard modern way to load a drawable resource is ContextCompat.getDrawable. It handles theme-aware loading and avoids older deprecated patterns.
If the resource ID is valid, this returns a ready-to-use Drawable. If the resource cannot be found, the result is null, so handle that case when loading user-selected or optional assets.
Different Resource Types, Same Loading API
The same loading call works for several drawable resource types:
- bitmap files such as PNG or JPEG
- vector drawables defined in XML
- shape drawables defined in XML
- state list drawables for pressed and selected states
- color drawables referenced from resources
That consistency is why ContextCompat.getDrawable is usually enough for normal app code. Android chooses the correct underlying Drawable subclass for you.
Apply the Drawable to Views
Once loaded, the drawable can be applied to different widgets depending on the UI need.
The runtime type may vary, but the usage pattern is stable because all of them inherit from Drawable.
Mutate Before Editing Shared State
A subtle Android behavior catches many developers: drawables loaded from resources may share a constant state. If you tint or modify one instance, another view using the same resource can change too.
Call mutate() before applying changes that should stay local to one view.
This matters most when a single resource is reused in lists, toolbars, or stateful controls.
Loading Drawables in Custom Views
Inside a custom view, use the view context rather than trying to reach for a global resource loader. That keeps theming correct and makes the view reusable.
This is a real creation point in practice: you resolve the resource into a runtime drawable and draw it yourself.
When You Need a Bitmap Instead
Sometimes the caller does not actually need a Drawable. If later code will manipulate pixels directly, pass data into a Bitmap pipeline instead. Converting a drawable to a bitmap just because a tutorial said so adds work and can waste memory.
Use a drawable when the job is display-oriented. Use a bitmap when the job is pixel-oriented.
Theme and Resource Qualifiers Matter
A drawable resource name does not map to one file only. Android may choose a different file based on density, night mode, locale, or other qualifiers. That is another reason to load through the resource system rather than opening files manually.
For example, R.drawable.logo might resolve to different assets on different devices. The code stays the same, and the framework picks the right version.
Common Pitfalls
A common mistake is using deprecated APIs such as resources.getDrawable without a theme argument. That can break tinting or behave differently across API levels.
Another issue is forgetting that getDrawable may return null. If the resource is optional, code defensively instead of assuming success.
Developers also run into shared-state bugs by tinting a resource without calling mutate(). The effect often shows up only after the same drawable is reused somewhere else.
Finally, do not decode every image into memory if a simple drawable reference on an ImageView is enough. Let the platform handle the normal resource path unless you have a measured need for custom bitmap processing.
Summary
- Load drawable resources with
ContextCompat.getDrawable. - The same API works for bitmap, vector, shape, and state list resources.
- Call
mutate()before local tint or state changes. - Use the current
contextso theming and qualifiers resolve correctly. - Only switch to bitmap processing when you truly need pixel-level work.
Related reading
- How to create EditText with crossx button at end of it?
- How to create EditText with rounded corners?
- How to create empty constructor for data class in Kotlin Android
- How to create fixed space and flexible space bar button items programmatically?
- How to create .ipa file using Xcode?
- How to create IPA in Xcode 6?
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.