Explain Codes LogoExplain Codes Logo

How to set background drawable programmatically in Android

java
android-development
performance-optimization
best-practices
Nikita BarsukovbyNikita BarsukovΒ·Sep 17, 2024
⚑TLDR

To set a background drawable to an Android view programmatically, you can use the following code:

// Find that button like it's a game of Where's Waldo Button myButton = findViewById(R.id.my_button); // Rydell High's resident bad boy Danny Zuko said it best, // "You're the (drawable) that I want" 🎡🎡R.drawable.my_drawable 🎡🎡 myButton.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.my_drawable));

Replace myButton and my_drawable with your respective ID and drawable. For those adventurous folks still rocking devices fossilized in pre-API 16 times, don't fret, there's this option too:

//Old school cool myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_drawable));

Make sure to replace myButton and my_drawable with your button's and drawable's actual names. Talk about a personal touch, right?

As Android versions get more numerous than Star Trek sequels, here are some best practices:

  • ContextCompat.getDrawable(): No need to feel like Goldilocks with this one. It helps find the drawable that’s just right for the Android version you're dealing with.

  • ResourcesCompat.getDrawable(): Like a time lord, it transcends time and space (or at least Android versions)

  • AppCompatResources.getDrawable(): Your golden ticket for AndroidX compatibility

And remember, folks, using getResources().getDrawable() without the fab three above is like bringing Spock to a Sith fight: it's deprecated and incompatible with newer API versions.

Working with large drawables

Feeling like Atlas, carrying the world of large bitmaps and complex drawables? Let these practices be your Hercules:

  • Performance Optimization: Nobody likes a "Sorry! The app stopped working" pop up. Ensure you follow Android's advice on loading large bitmaps efficiently to avoid tripping over OOM errors.

  • Lifecycle consciousness: Be the lifecycle whisperer and set backgrounds in the onCreate() or onResume() functions. It's best to tickle the dragon's tail when it's sure to be seen.

Adding a touch of diligence

Here's what your checklist should look like when dealing with drawables:

  • Global references: To access views across different methods, declare them globally. Because who wants to play hide n seek with views?

  • Null checks: Null errors are the scarecrows in fields of code; don't forget to guard against them.

  • Layout references: Check and recheck! Ensure the correct view reference is being modified.

  • Usage of setContentView(): Think of this as serving the main course; do it after setting the background to savor the full effect.

  • Support library inclusion: Don't leave the support-v4 library (or equivalent AndroidX libraries) behind. Like a passport, it unlocks compatibility doors.

  • SDK best practices: Familiarity with target SDK version practices ensures your app plays nice across different Android versions.