Android Development
Programming
Mobile App Development
Android UI Design
Java

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:

  1. Using Resource IDs: The simplest way to set a background drawable is by using an existing drawable resource. For example:
java
   View view = findViewById(R.id.my_view);
   view.setBackgroundResource(R.drawable.background_image);

Here, R.drawable.background_image refers to a drawable resource located in the /res/drawable directory.

  1. Creating Drawables Programmatically: You can also create drawables in code. For instance, creating a ColorDrawable which just displays a color:
java
   ColorDrawable colorDrawable = new ColorDrawable(Color.RED);
   view.setBackground(colorDrawable);

This sets the background of the view to a solid red color.

  1. Using GradientDrawable: For more complex shapes and gradients, GradientDrawable is very useful:
java
1   GradientDrawable gradientDrawable = new GradientDrawable(
2       GradientDrawable.Orientation.TOP_BOTTOM,
3       new int[] {Color.RED, Color.BLUE});
4   gradientDrawable.setShape(GradientDrawable.RECTANGLE);
5   gradientDrawable.setCornerRadius(8);
6   view.setBackground(gradientDrawable);

This code snippet creates a gradient from red to blue for the background of the view and sets the corner radius to 8dp.

  1. Handling Bitmaps: If you need to set a bitmap as a background (e.g., an image downloaded from the web), you can use BitmapDrawable:
java
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
   BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
   view.setBackground(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 VectorDrawable for scalable graphics without loss of resolution.

Summary Table of Methods

MethodUse CaseCode Example
setBackgroundResource(int)Simple use with existing drawable resourcesview.setBackgroundResource(R.drawable.bg);
setBackground(Drawable)Programmatic or dynamic backgroundsview.setBackground(new ColorDrawable(Color.RED));
GradientDrawableCustom shapes and gradientsview.setBackground(gradientDrawable);
BitmapDrawableUse bitmaps as backgroundview.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.


Course illustration
Course illustration

All Rights Reserved.