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:
This work for me:
yourview.setBackground(null);
Try this
RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29);
relative.setBackgroundResource(0);
Check the setBackground functions in the RelativeLayout documentation
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.
I try this code in android 4+:
view.setBackgroundDrawable(0);
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.
Best performance on this method :
imageview.setBackgroundResource(R.drawable.location_light_green);
Use this.