How set background drawable programmatically in Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Android development, setting background drawables programmatically is a common requirement when you have to dynamically adjust the UI based on the application's state or user interactions. This can greatly enhance the flexibility and visual appeal of an app. There are several ways to set a background drawable on a View programmatically, and choosing the right method can depend on various factors including performance implications and specific use cases.
Understanding Background Drawables
A "drawable" in Android is a general abstraction for something that can be drawn. This could include static images, dynamic graphics, shapes, gradients, and more. These can be set as backgrounds to almost any View in Android. Background drawables can be specified in XML layout files or directly in code.
Setting Drawable Backgrounds Programmatically
To set a background drawable programmatically, you typically manipulate the background property of a View. Here's how you can achieve this:
- Using Resource IDs: The simplest way to set a background drawable is by using an existing drawable resource. For example:
Here, R.drawable.background_image refers to a drawable resource located in the /res/drawable directory.
- Creating Drawables Programmatically: You can also create drawables in code. For instance, creating a
ColorDrawablewhich just displays a color:
This sets the background of the view to a solid red color.
- Using
GradientDrawable: For more complex shapes and gradients,GradientDrawableis very useful:
This code snippet creates a gradient from red to blue for the background of the view and sets the corner radius to 8dp.
- Handling Bitmaps: If you need to set a bitmap as a background (e.g., an image downloaded from the web), you can use
BitmapDrawable:
Performance Considerations
When using drawables programmatically, especially within UI components that are visible in lots of instances (e.g., items in a RecyclerView), it's crucial to consider the impact on performance and memory:
- Reuse drawables if possible rather than creating new instances.
- Avoid using large bitmap images as backgrounds in repeated views.
- Consider using
VectorDrawablefor scalable graphics without loss of resolution.
Summary Table of Methods
| Method | Use Case | Code Example |
setBackgroundResource(int) | Simple use with existing drawable resources | view.setBackgroundResource(R.drawable.bg); |
setBackground(Drawable) | Programmatic or dynamic backgrounds | view.setBackground(new ColorDrawable(Color.RED)); |
GradientDrawable | Custom shapes and gradients | view.setBackground(gradientDrawable); |
BitmapDrawable | Use bitmaps as background | view.setBackground(bitmapDrawable); |
Additional Tips
- Dynamic Adjustments: Listen to app state changes or user interactions to dynamically change backgrounds. Use
setState()or similar methods on drawables if they support various states. - Custom Drawables: For completely custom drawing logic, consider subclassing
Drawable. - Preloading Resources: To reduce load times, pre-load resources in advance if you expect to switch backgrounds frequently.
Setting backgrounds programmatically can be a powerful way to enhance your app's interactivity and polish. By understanding the options and trade-offs, you can use these techniques effectively to create compelling Android applications.

