153

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>
Max Base
  • 639
  • 1
  • 7
  • 15
Emkey
  • 5,346
  • 8
  • 38
  • 55

11 Answers11

363

Try this

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

Check the setBackground functions in the RelativeLayout documentation

Maragues
  • 37,861
  • 14
  • 95
  • 96
  • 4
    I get this error: The method setBackgroundResource(int) in the type View is not applicable for the arguments (null) – UKDataGeek May 13 '12 at 09:08
  • 2
    In case that doesn't work: check if you've used the background property and not android:src! – Chris Oct 16 '12 at 23:00
  • 3
    setBackgroundDrawable is now deprecated. @Suraj's answer below is better now. – Anand Sainath Feb 17 '13 at 11:19
  • 6
    api 16? is there anything for api 8? – user1940676 Sep 24 '13 at 11:10
  • try setBackgroundDrawable(null) – Maragues Sep 24 '13 at 11:23
  • If the view is embedded in a larger layout this may harm the overall layout (faced this issue when using a `TableLayout`). Replacing `@drawable/bg` by a comparable (in terms of size) transparent drawable resolves this issue. – Trinimon Jan 16 '14 at 13:26
  • This will not work if your `minSdkVersion` is lower than 16. To support earlier versions, see my answer. – Adam Stelmaszczyk Mar 21 '14 at 13:06
  • 1
    Use setBackgroundResource(0). as Answered by @AdamStelmaszczyk. , – Zar E Ahmer Jul 04 '14 at 06:22
  • but using setBackgroundResource(0) will remove the outer layer of edit text, suppose I'm using a drawable for error display and when user is correct then it will remove the drawable along with the default outer line of edit text, i am working on api 8, any solution ? – Rishabh Agrawal Feb 03 '16 at 10:45
  • @Maragues this is logically solution. But why after doing this memory usage doesn't decrease? (I'm using Android Studio Memory Monitor) – Volodymyr Kulyk Jul 14 '16 at 09:14
  • @VladimirKulyk that's a good question, I haven't tested myself and I don't have the answer. Quick ideas that come to my mind. Have you tried to force a GC? (you can do that from AS). Also might be due to some internal resource caching. If it's a large bitmap, try holding a reference and force a recycle after removing the background. – Maragues Jul 14 '16 at 09:30
  • It's not working now at least for me. – mazend Aug 22 '22 at 08:32
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.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • 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 May 04 '14 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 May 29 '14 at 20:52
48

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

Suraj Bajaj
  • 6,630
  • 5
  • 34
  • 49
7

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); 
Mohamed Salah
  • 868
  • 1
  • 15
  • 34
Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
4

I try this code in android 4+:

view.setBackgroundDrawable(0);
Max Base
  • 639
  • 1
  • 7
  • 15
Salman666
  • 81
  • 3
  • The method setBackgroundDrawable(Drawable) in the type View is not applicable for the arguments (int) – Tobrun Jun 26 '13 at 08:42
3

Best performance on this method :

imageview.setBackgroundResource(R.drawable.location_light_green);

Use this.

Mahmudul
  • 470
  • 1
  • 4
  • 12
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.

Jeel Shah
  • 3,274
  • 17
  • 47
  • 68
1

This work for me:

yourview.setBackground(null);
baikho
  • 5,203
  • 4
  • 40
  • 47
lscofield
  • 25
  • 2
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.

whoan
  • 8,143
  • 4
  • 39
  • 48
anand krish
  • 4,281
  • 4
  • 44
  • 47
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);
Đorđe Nilović
  • 3,600
  • 1
  • 25
  • 20
-3

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
Max Base
  • 639
  • 1
  • 7
  • 15
SRam
  • 2,832
  • 4
  • 45
  • 72