Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setImageDrawable vs setBackground for placeholder

Tags:

java

android

I am trying to find the right approach in setting the placeholder image/resource (VectorDrawable resource in my case) for an ImageView that gets set, with the target image bitmap when it is available.

There are two approaches I saw in guides. I can either set a background using the background property on XML or programmatically using setBackground(). Then, I would set the target image using setImageDrawable().

The second approach is to use setImageDrawable() for the placeholder and later use setImageDrawable() again for the target image.

Both approaches work but I have noticed some UI lag when using the first approach. I am not sure if it is caused by the approach or not.

So, I want to know. What is the correct approach in using placeholders and why?

Thank you.

like image 574
Tiano Manti Avatar asked May 02 '26 22:05

Tiano Manti


1 Answers

The second approach is always the best for this task. But still, if you want to go with the first approach then the correct way to use it is like this

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable();
} else {
    setBackground();
}
like image 109
GOVIND DIXIT Avatar answered May 05 '26 10:05

GOVIND DIXIT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!