Khawaja Absar
Khawaja Absar

Reputation: 135

setBackgroundDrawable in android

I want to set background drawable of a imagebutton. There are two methods for this (as far as I see): setBackground and setBackgroundDrawable. I am using setBackground, it says it has been added in API level 16 but my project's min SDK version is 7. For this i am using setBackgroundDrawable, but its is not changing the drawable at run time in api lower then 16?This is my code snippet

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")

private void setVariable() {
        try {

        //change the bg for save button
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        //register.setBackground(getResources().getDrawable(R.drawable.btn_save));
                register.setBackgroundDrawable(getResources().getDrawable(R.drawable.btnsave));
            }else{
                register.setBackground(getResources().getDrawable(R.drawable.btn_save));
            }
            //register.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_save));
        } catch (Exception e) {
            // TODO: handle exception
    }
}

What am I supposed to use ?

Upvotes: 1

Views: 7372

Answers (3)

user957654
user957654

Reputation:

try the following :

btn.setBackgroundResource(R.drawable.new_todo_image);

and give some feedback

Hope that helps .

Upvotes: 1

Anuj
Anuj

Reputation: 21

You are mistaking while writing resource id of drawable.
You're using R.id.btnsave instead of *'R.drawable.btn_save'*
Otherwise, your code is absolutely right!

Upvotes: 1

Sujith Thankachan
Sujith Thankachan

Reputation: 3506

Use register.setBackgroundResource(R.drawable.btn_save);.

public void setBackgroundResource (int resid) is available from API Level 1.

Upvotes: 2

Related Questions