How to set background color of a View

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

I use this code:

  • How can I crop a bitmap for ImageView?
  • Display Logcat in Terminal?
  • get tilt angle Android
  • Android: detect brightness (amount of light) in phone's surroundings using the camera?
  • How to define dimens.xml for every different screen size in android?
  • Adding a button to a custom InfoWindowAdapter view that can register clicks
  • // 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.

    Related posts:

    How to stop ActionBar Tab navigation to display as a spinner when it gets too long?
    How to add a notification settings activity to the system settings
    Visual Studio 2015 does not install Android SDK for Xamarin
    Can't resize a RelativeLayout inside a ScrollView to fill the whole screen
    How to add a hyperlink into a Preference Screen (PreferenceActivity)
    how print local timezone in posix format in java(android)?
  • How to Dismiss/Cancel the status bar notification in android programmatically
  • Where is the location of file created by Cordova File Plugin?
  • Create “ok glass” style menu, within glass app
  • Gradle sync failed: failed to find Build Tools revision 24.0.0 rc1
  • When would Fragment's onDestroyView be called, but it wouldn't be destroyed?
  • Type Conversion from C to Java
  • 13 Solutions collect form web for “How to set background color of a View”

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

    Try v.setBackgroundColor(0xFF00FF00);

    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.

    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));
    

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

    View.setBackgroundColor(Color.parseColor("#e7eecc"));
    
    // 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.

    and what is the correct way to change
    the background color on any View?

    On any View? What you have is correct, though you should drop the invalidate() call.

    However, some Views already have backgrounds. A Button, for example, already has a background: the face of the button itself. This background is a StateListDrawable, which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.

    In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button.

    For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:

    android:background="#FF00FF00"
    

    try to add:

    setBackgroundColor(Color.parseColor("#FF0000"));
    

    I use at API min 16 , target 23

    Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);
    
    WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
    

    This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.

    You can simple use :

    view.setBackgroundColor(Color.parseColor("#FFFFFF"));
    
    mButton.setBackgroundColor(getResources().getColor(R.color.myColor));
    

    Stating with Android 6 use ContextCompact

            view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.