154

I want to remove the background drawable @drawable/bg programmatically. Is there a way to do that?

Currently, I have the following XML in my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/widget29"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg">

</RelativeLayout>
0

11 Answers 11

365

Try this

RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29);
relative.setBackgroundResource(0);

Check the setBackground functions in the RelativeLayout documentation

12
  • 4
    I get this error: The method setBackgroundResource(int) in the type View is not applicable for the arguments (null)
    – UKDataGeek
    Commented May 13, 2012 at 9:08
  • 2
    In case that doesn't work: check if you've used the background property and not android:src! Commented Oct 16, 2012 at 23:00
  • 3
    setBackgroundDrawable is now deprecated. @Suraj's answer below is better now. Commented Feb 17, 2013 at 11:19
  • 6
    api 16? is there anything for api 8? Commented Sep 24, 2013 at 11:10
  • 1
    Use setBackgroundResource(0). as Answered by @AdamStelmaszczyk. , Commented Jul 4, 2014 at 6:22
78

setBackgroundResource(0) is the best option. From the documentation:

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

It works everywhere, because it's since API 1.

setBackground was added much later, in API 16, so it will not work if your minSdkVersion is lower than 16.

2
  • 2
    Thank you, totally saved me after 4 hours of head-banging. This is very useful in case you dynamically create a toggled button (choice of 2 drawables) in an onClick event, but need a visible button to show before your toggled ones take over (because nothing will show until the click event happens, so when it does, you can use setBackgroundResource(0) to remove your earlier "set" button drawable).
    – Azurespot
    Commented May 4, 2014 at 23:10
  • 2
    This should be the chosen answer because it is best solution both in terms of memory management and api level support.
    – gregtzar
    Commented May 29, 2014 at 20:52
48

This helped me remove background color, hope it helps someone. setBackgroundColor(Color.TRANSPARENT)

2
  • 3
    Drawables can have padding, so if you remove background, you need to remove padding too. Commented Oct 13, 2014 at 17:30
  • 1
    setBackgroundResource(0) is the best option because, it removes the background completely hence, reducing overdraw.
    – Milan
    Commented Jan 26, 2016 at 4:02
8

Try this code:

imgView.setImageResource(android.R.color.transparent); 

also this one works:

imgView.setImageResource(0); 

but be careful this one doesn't work:

imgView.setImageResource(null); 
5

I try this code in android 4+:

view.setBackgroundDrawable(0);
2
  • The method setBackgroundDrawable(Drawable) in the type View is not applicable for the arguments (int)
    – Tobrun
    Commented Jun 26, 2013 at 8:42
  • should've been setBackgroundResource(0)
    – DTechnlogy
    Commented Sep 2 at 6:09
3

Best performance on this method :

imageview.setBackgroundResource(R.drawable.location_light_green);

Use this.

1

In addition to the excellent answers, if you want to achieve this via xml then you can add:

android:background="@android:color/transparent

to your view.

1

This work for me:

yourview.setBackground(null);
0
0

Use setBackgroundColor(Color.TRANSPARENT) to set the background as transparent, or use setBackgroundColor(0). Here Color.TRANSPARENT is the default attribute from color class. It will work fine.

0

I have a case scenario and I tried all the answers from above, but always new image was created on top of the old one. The solution that worked for me is:

imageView.setImageResource(R.drawable.image);
-4

First, you have to write in XML layout:

android:visibility="invisible" <!--or set VISIBLE-->

then use this to show it using Java:

myimage.setVisibility(SHOW); //HIDE
1
  • What is it? What for?
    – CoolMind
    Commented Feb 14, 2018 at 9:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.