0

I must be overthinking this one, but when I am trying to change the background of a relative layout onClickI am getting the error setBackground in view cannot be set to int. Here is some of the code below:

btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!check) {
                check = true;
                relativeLayout.setBackground(R.drawable.bg);
                                 ^^^Apparently this is considered an int???
            } else {
                relativeLayout.setBackgroundColor(Color.WHITE);
                check = false;
            }
        }
    });

Some things to note:

  • The relative layout is attributed to a class that extends a fragment
  • Btn1 and relativeLayout were correctly identified outside of this small section of code. I tried looking around the website for similar problems but could only find one. The solution was to use Bitmap Factory and set the Background to Bitmap bg. This wasn't the solution for me. Also, the background I am trying to reach has been placed in the drawable folder, with an all lowercase name, as a .png. Also, I have build, rebuilt, and cleaned the project--same error. Help?
Ethan
  • 1,905
  • 2
  • 21
  • 50

3 Answers3

2

R.(anything).(anything)

is not the specific object, its the address to where the resource resides from perspective of your compiled resources. It is not the drawable, when you use this id with respect to your context APIs you can get a drawable from that id.

What you want is

(View).setBackgroundResouce(id)

http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)

What you are using is http://developer.android.com/reference/android/view/View.html#setBackground(android.graphics.drawable.Drawable)

Which is expecting a drawable. Read the APIs to understand how and when to use them.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • Tried `relativeLayout.setBackgroundResource(R.drawable.bg);` didn't work – Ethan Feb 25 '16 at 06:22
  • 1
    didn't work doesn't tell us anything. You must be specific. If it didn't work then its an ENTIRELY different reason from the APIs that you are using. Chances are its something wrong with your code. Add logs and debug it to figure it out. Your main question was answered, ideally you should close the question and debug the rest yourself. – JoxTraex Feb 25 '16 at 06:23
0

Use setBackgroundResource() method instead. Try the following code:

relativeLayout.setBackgroundResource(R.drawable.bg);

Or try the following:

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.bg);
relativeLayout.setBackground(drawable);
Kanchan Chowdhury
  • 433
  • 1
  • 5
  • 15
0

setBackground takes Drawable and not resource id. When you write R.drawable.bg it will return the resource id (int).

So setting background using the image defined in resources can be done in two ways,

using setBackground

relativeLayout.setBackground(getResources().getDrawable(R.drawable.bg))

using setBackgroundResource

relativeLayout.setBackgroundResource(R.drawable.bg);
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • as @JoxTraex mentioned *didn't work doesn't tell us anything. You must be specific. If it didn't work then its an ENTIRELY different reason from the APIs that you are using. Chances are its something wrong with your code. Add logs and debug it to figure it out.* – Rajen Raiyarela Feb 25 '16 at 06:28
  • Sorry about that, it has been getting to me for a while now. Everything is rigged up correctly on the code side, but I am wondering if the `relativeLayout` has to have a predefined background int he xml, even if it just is `android:background="#ffffff"` – Ethan Feb 25 '16 at 06:36
  • Any ideas if this is the case? – Ethan Feb 25 '16 at 06:37
  • no it is not required to have any predefined background it app. You can set directly using code as well. Check your logcat there must be some error mentioned. – Rajen Raiyarela Feb 25 '16 at 06:39