How to set background color of a View

484,881

Solution 1

You made your button transparent. The first byte is the alpha.

Try v.setBackgroundColor(0xFF00FF00);

Solution 2

When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource...

View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);

Experiment with PorterDuff.Mode.* for different effects.

Solution 3

Several choices to do this...

Set background to green:

v.setBackgroundColor(0x00FF00);

Set background to green with Alpha:

v.setBackgroundColor(0xFF00FF00);

Set background to green with Color.GREEN constant:

v.setBackgroundColor(Color.GREEN);

Set background to green defining in Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>     
    <color name="myGreen">#00FF00</color> 
    <color name="myGreenWithAlpha">#FF00FF00</color> 
</resources>

and using:

v.setBackgroundResource(R.color.myGreen);

and:

v.setBackgroundResource(R.color.myGreenWithAlpha);

or the longer winded:

v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));

and:

v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));

Solution 4

You can set the hex-color to any resource with:

View.setBackgroundColor(Color.parseColor("#e7eecc"));

Solution 5

// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();

The code does not set the button to green. Instead, it makes the button totally invisible.

Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.

The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.

Share:
484,881

Related videos on Youtube

Peter vdL
Author by

Peter vdL

This isn't about me. This is about us. we. you. The whole gestalt of wax. Never dereference a null pointer - no good will come of it.

Updated on February 13, 2021

Comments

  • Peter vdL
    Peter vdL over 3 years

    I'm trying to set the background color of a View (in this case a Button).

    I use this code:

    // set the background to green
    v.setBackgroundColor(0x0000FF00 );
    v.invalidate();
    

    It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?

    Thanks.

    • RickNotFred
      RickNotFred over 14 years
      That looks correct. In fact, I don't think you need to call invalidate(). When you say the button disappears, do you mean that literally or do you mean the button had text or an image on it that disappears?
  • Peter vdL
    Peter vdL over 14 years
    OK, thanks for the explanation about Button backgrounds. Nine patch bitmaps (developer.android.com/reference/android/graphics/NinePatch.‌​html) were new to me. I simply want to change the color of anything on the screen when I press a Button. The Buttons are on a TextView. Trying to change the color of that, leads to console messages "DDM dispatch reg wait timeout... ActivityManager: Can't dispatch DDM chunk 52454151: no handler defined" and a dialog on screen "the app stopped unexpectedly". Guess I need to do more reading on the UI. Any hints welcome. Thanks.
  • Molten Ice
    Molten Ice almost 10 years
    EddieB's answer below is much better as this removes any borders etc. such as an EditText's underlining
  • Admin
    Admin almost 9 years
    @aroth is better Color.Green ? If I want color complext with argb ? ... your answer is deprecated
  • aroth
    aroth almost 9 years
    @delive - Come again? Color.GREEN is not deprecated, and includes the alpha byte. It's value is 0xff00ff00, exactly the same as what's suggested in the answer, except it also has the benefit of being human-readable. Most developers would prefer a human-readable solution.
  • Yousha Aleayoub
    Yousha Aleayoub over 8 years
    "overwrites/removes any existing background resource," not really.
  • Junior Mayhé
    Junior Mayhé over 8 years
    getResources().getColor(...) got deprecated stackoverflow.com/questions/31842983/…
  • grim
    grim about 8 years
    There's also: v.setBackgroundResource(R.color.myGreen); when using colors.xml
  • Hashim Akhtar
    Hashim Akhtar about 8 years
    I had to use the above along with v.invalidate(); on the next line.
  • Krystian
    Krystian over 7 years
    Usually there are not a lot of default colors. New colors can be defined in res/values/colors as described here developer.android.com/samples/BasicMediaRouter/res/values/…
  • weston
    weston about 7 years
    Removed the invalidate calls, as you can see setBackground... methods already call that.
  • weston
    weston about 7 years
    v.setBackgroundColor(0x00FF00); is just their code which they say isn't working. I would also expect it to be transparent.
  • Siddarth G
    Siddarth G almost 7 years
    tried this wasted time and went with the accepted answer, v.getBackground() gave nullpointer exception and crashed.
  • Guilherme Carvalho
    Guilherme Carvalho about 5 years
    This plus PorterDuff.Mode.SRC_IN solved for me so I wouldn't lose my background border.
  • Peter vdL
    Peter vdL about 5 years
    The question makes clear that the color change needs to be dynamic.
  • Steven
    Steven over 2 years
    What do you write between the brackets when you want to refer to a color in your colors.xml?