Android. Change the background color of a FrameLayout from code

I’m trying to change the background color of a FramyLayout.
The color is changing, but to the wrong one.

However it is working fine if I do it through the XML.

  • Getting the current position of a ViewPager
  • Android: How to enable/disable option menu item on button click?
  • How to pass parameter to a webservice using ksoap2?
  • Failure when trying to install android apk package using adb for platform 4.1
  • Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat
  • Create submenus programmatically for existing Menu created from XML
  • Here is my res/values/colors.xml code

    <?xml version="1.0" encoding="utf-8"?>
        <color name="grey">#888888</color>
        <color name="white">#FFFFFF</color>
        <color name="red">#ffff3448</color>
        <color name="green">#ff408c3a</color>

    Here is how it looks like if I make changes in the XML

    Colors changed correctly

    And that’s what is happening if I try to do it with the code

     FrameLayout frameLayoutBalance = (FrameLayout)view.findViewById(;

    Colors changed incorrectly

    Related posts:

    Android: Difference between positive, negative, and neutral button
    android color between two colors, based on percentage?
    Gradle error : Write access is allowed from event dispatch thread only in android studio
    Convert base64 string to Image in Java
    How to set images for ScrollView instead-of fading edges?
    android ArrayAdapter items update
  • Android : detect click event at empty space of gridview inside linearlayout
  • Is there a better way to refresh WebView?
  • Automatic layout mirroring in right-to-left locale with lollipop
  • Can't change default output folder
  • How to show pop up on clicking map overlay?
  • Is there a way to use features in Android 2.1/2.2 while keeping a minSDK version to 3?
  • 2 Solutions collect form web for “Android. Change the background color of a FrameLayout from code”

    You should not use

    setBackgroundColor required a Color (i.e. its value as describe by Chirag Raval) not a color resources.

    use this frameLayoutBalance.setBackgroundColor(getResources().getColor(;

    Use this code

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.