-5

How to delete the image of BackgroundResource from button? For example: b.setBackgroundResource(R.drawable.breakdown) I'm sure that setBackgroundResource(0) is not available! Thank you

Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
游智博
  • 11
  • 1
  • 1
    `I'm sure that setBackgroundResource(0) is not available!` why? – Vladyslav Matviienko Aug 03 '17 at 09:09
  • 1
    Possible duplicate of [How to remove ImageButton's standard background image?](https://stackoverflow.com/questions/5457138/how-to-remove-imagebuttons-standard-background-image) – Adinia Aug 03 '17 at 09:17

3 Answers3

0

try this using this you will get default button style.

btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));

or this

b.setBackgroundResource(null);

or this

b.setBackgroundResource(0);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • @游智博 did you try this `btn.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));` – AskNilesh Aug 03 '17 at 08:57
  • yes, it would reset the all button's property and style, but I just want to stop showing image – 游智博 Aug 03 '17 at 09:07
0

Try:

b.setBackground(null);

The documentation states:

Set the background to a given Drawable, or remove the background.

Also:

b.setBackgroundResource(0);

The documentation states:

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Edit: Because you stated that it does not work, I wrote this to test it, and it works perfectly:

final Button btnTest = (Button) findViewById(R.id.btn_test);
btnTest.setBackgroundResource(R.drawable.some_drawable);
Button btnClean = (Button) findViewById(R.id.btn_clean);
btnClean.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
          btnTest.setBackground(null);
     }
});
Matias Olocco
  • 509
  • 4
  • 9
  • That cannot be. I updated my answer, I just wrote a small piece of code to test it and it works perfectly. – Matias Olocco Aug 03 '17 at 09:08
  • 1
    Also, as someone else said, you can use setBackgroundResource(0); this exists and is usable, from doc: "Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background." – Matias Olocco Aug 03 '17 at 09:12
  • Still no reaction – 游智博 Aug 03 '17 at 09:30
  • It is as same as yours – 游智博 Aug 03 '17 at 09:39
  • No, please, post the code that you have, you cannot say that is the same when mine is working. What is your context? where do you have the button? is it on a list? recyclerview? in a basic layout?. Please post the xml and the code as an edit in the original question. Otherwise we won't be able to help you. – Matias Olocco Aug 03 '17 at 09:50
0

Put this code...

b.setBackgroundResource(null);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57