45

How Do I set the background color of my android app. When I try:

LinearLayout li=(LinearLayout)findViewById(R.id.myLayout);
li.setBackgroundColor(Color.parseColor("#rrggbb"));

My app always crashes. Could someone help me out. Thanks

2
  • why it crashes? what does logcat says? I believe that it is due to something else, like for example, do you set a layout to your Activity by using setContentView method ? Aug 3, 2013 at 13:17
  • 1
    Do you actually use Color.parseColor("#rrggbb") ? You should use hex there Aug 3, 2013 at 13:20

5 Answers 5

74
Color.parseColor("#rrggbb")

instead of #rrggbb you should be using hex values 0 to F for rr, gg and bb:

e.g. Color.parseColor("#000000") or Color.parseColor("#FFFFFF")

Source

From documentation:

public static int parseColor (String colorString):

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'

So I believe that if you are using #rrggbb you are getting IllegalArgumentException in your logcat

Source

Alternative:

Color mColor = new Color();
mColor.red(redvalue);
mColor.green(greenvalue);
mColor.blue(bluevalue);
li.setBackgroundColor(mColor);

Source

29

This question is a old one but it can help for others too.

Try this :

    li.setBackgroundColor(getResources().getColor(R.color.blue));

    or

    li.setBackgroundColor(getResources().getColor(android.R.color.red));

    or

    li.setBackgroundColor(Color.rgb(226, 11, 11));


    or
    li.setBackgroundColor(Color.RED)
8

Try this:

li.setBackgroundColor(android.R.color.red); //or which ever color do you want

EDIT: Posting logcat file would also help.

1
  • 1
    mainLayout.setBackgroundColor(getResources().getColor(android.R.color.black));
    – fullmoon
    Aug 9, 2015 at 20:14
1

By the way, a good tip on quickly selecting color on the newer versions of AS is simply to type #fff and then using the color picker on the side of the code to choose the one you want. Quick and easier than remembering all the color hexadecimals. For example:

android:background="#fff"
1
  • The OP is asking how to set the background color of an element using Java, not XML. May 29, 2020 at 5:11
0
        int R = 111;    //My be assigned dynamic value
        int G = 111;    //My be assigned dynamic value
        int B = 111;    //My be assigned dynamic value
        
        getWindow().getDecorView().setBackgroundColor(Color.rgb(R, G, B));
2
  • Easy to use to change complete background-screen. Dec 19, 2021 at 10:44
  • Try to not write only the code as answer but also explain it at least briefly as well
    – void
    Dec 20, 2021 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.