removing backgroundcolor of a view in android
Asked Answered
C

5

48

Removing Background color in Android

I have set backgroundColor in code like this,

View.setBackgroundColor(0xFFFF0000);

How to remove this background color on some event?

Coextensive answered 29/10, 2010 at 10:31 Comment(4)
You could set it to transparent: View.setBackgroundColor(0x00000000); Or android.R.color.transparentMultilingual
just found out that , another way to set it to transparent would be , View.setBackgroundColor(Color.TRANSPARENT);Coextensive
If you just want to use the parent's background, you can remove the entire child's background with: view.setBackgroundResource(0);Basis
donot works for me toolbar.setBackgroundColor(Color.TRANSPARENT); @Coextensive donot works for me toolbar.setBackgroundColor(0); Also View.setBackgroundColor(0x00000000); donot works for meTaveras
S
61

You should try setting the background color to transparent:

view.setBackgroundColor(0x00000000);

Slake answered 29/10, 2010 at 10:38 Comment(2)
Ok , I thought there will be something to remove backgroundcolor property.Coextensive
Since it's an int I don't think you can sort of "null" it out, this answer should work fine with 0x00AAF890 also (or any 0x00------) since the first two 0's set the opacity to 0% which is to say there is not background color.Heti
A
51

You can use

View.setBackgroundColor(Color.TRANSPARENT);

or

View.setBackgroundColor(0);

or even

View.setBackground(null);

Please remember that almost everything visible on the screen extends View, like a Button, TextView, ImageView, any kind of Layout, so this can be done to almost everything.

Bonus point!

Instead of using View.setBackgroundColor(0xFFFF0000); it is sooooooooo much better to create a color filter with android.graphics.PorterDuff.MULTIPLY option and, if needed, just clear the filter.

Using the color filter, colors look much better because it won't be that "flat" color but a nicer tone of the chosen color.

A light red background would be View.getBackground().setColorFilter(0x3FFF0000, PorterDuff.Mode.MULTIPLY);

To "remove" the color, just use View.getBackground().clearColorFilter();

Abound answered 21/1, 2016 at 17:32 Comment(2)
Color.TRANSPARENT looks much cleaner than 0x0000000. You have my upvote!Mcafee
Thanks @Teilmann. I also added the "Color Filter" option that, in my opinion, looks much better compared to setBackground. You should try it!Abound
E
17

All of the answers about setting color to transparent will work technically. But there are two problems with these approaches:

  1. You'll end up with overdraw.
  2. There is a better way:

If you look at how View.setBackgroundColor(int color) works you'll see a pretty easy solution:

/**
 * Sets the background color for this view.
 * @param color the color of the background
 */
@RemotableViewMethod
public void setBackgroundColor(@ColorInt int color) {
    if (mBackground instanceof ColorDrawable) {
        ((ColorDrawable) mBackground.mutate()).setColor(color);
        computeOpaqueFlags();
        mBackgroundResource = 0;
    } else {
        setBackground(new ColorDrawable(color));
    }
}

The color int is just converted to a ColorDrawable and then passed to setBackground(Drawable drawable). So the solution to remove background color is to just null out the background with:

myView.setBackground(null);
Engraving answered 4/5, 2017 at 18:11 Comment(4)
This is the appropriate answer. Making the background transparent requires processing time, while removing the background doesn't.Maryettamaryjane
setBackground is from API 16, how can I do that for lower APIs?Tamayo
for lower api you should use myView.setBackgroundColor(Color.TRANSPARENT);Aveyron
this should be accepted answer, because transparent background has hidden costsAgraphia
Q
6

View.setBackgroundColor(0); also works. It isn't necessary to put all those zeros.

Quadrivium answered 18/8, 2015 at 19:12 Comment(0)
E
0

Choose Any one

View.setBackgroundColor(Color.TRANSPARENT);

    or

    View.setBackgroundColor(0x00000000);
Eloiseloisa answered 28/9, 2018 at 10:12 Comment(1)
How does this add to the existing answers?Engraving

© 2022 - 2024 — McMap. All rights reserved.