0

I want to change the layout background by clicking a button, but a red underline occurs in layoutname.setBackground(drawable) which says that I should have minimum SDK version 16 and not 9 (which I have) My question is, the following code I use to check is proper?

Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.gradientblue);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
LinearForColor.setBackgroundDrawable(drawable);
           } else {
                LinearForColor.setBackground(drawable);
            }

1 Answers1

0

Why you aren't using setBackgroundResource() instead? It is supported in all the API levels (API1 to API21).

It is more simpler then the others. Rather than passing the drawable you have to pass the id of a drawable in it. For example what you are doing for setBackground and for setBackgroundDrawable is:

    LinearForColor.setBackgroundDrawable(getResources().getDrawable(R.drawable.myLogo));    
    LinearForColor.setBackground(getResources().getDrawable(R.drawable.myLogo));

For setBackgroundResource just pass the ID of the drawable like below:

LinearForColor.setBackgroundResource(R.drawable.myLogo);

So you don't need to be worried about the BuildVersion of the device. isn't it more convenient?

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50