Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between setBackgroundResource and setBackgroundDrawable

Could anyone tell me what is the difference between setBackgroundResource(resourceid) and setBackgroundDrawable(getResource().getDrawable(drawableid)) in android?

like image 402
sravanalakshmi.sunkara Avatar asked Aug 07 '13 10:08

sravanalakshmi.sunkara


People also ask

How to put image in Drawable XML file?

To use an image resource, add your file to the res/drawable/ directory of your project. Once in your project, you can reference the image resource from your code or your XML layout. Either way, it's referred to using a resource ID, which is the file name without the file type extension. For example, refer to my_image.

What is Drawable in android studio?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.


1 Answers

You can have a look at the Android source code for the View class yourself and find out that there is very little difference!

public void setBackgroundResource(int resid) {
    if (resid != 0 && resid == mBackgroundResource) {
        return;
    }

    Drawable d= null;
    if (resid != 0) {
        d = mResources.getDrawable(resid);
    }
    setBackground(d);

    mBackgroundResource = resid;
}

And setBackground() just calls through to setBackgroundDrawable()...

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}
like image 150
Alex MDC Avatar answered Nov 16 '22 02:11

Alex MDC