Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set background color: Android

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

like image 714
Cj1m Avatar asked Aug 03 '13 13:08

Cj1m


1 Answers

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

like image 163
Boris Mocialov Avatar answered Sep 22 '22 06:09

Boris Mocialov