How to set the background color for linear layout programmatically?

How to set the background color for linear layout programmatically? I tried the following code but did not work:

LinearLayout footer = new LinearLayout(activity); footer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 8)); footer.setBackgroundColor(Color.parseColor("##ffb5d6e1")); ((LinearLayout)v).addView(footer); 
+4
source share
3 answers

I think your problem is in your line:

 footer.setBackgroundColor(Color.parseColor("##ffb5d6e1")); 

Remove the extra '#' character so that it:

 footer.setBackgroundColor(Color.parseColor("#b5d6e1")); 

I also deleted 'ff' because you essentially set the opacity to 100%, which is done by default if you use only six-digit hexadecimal values.

+2
source

You are on the right track, but made a very minor mistake.

The color format you are using is incorrect. From Android white papers,

Supported formats: #RRGGBB #AARRGGBB or one of the following names: "red", "blue", "green", "black", "white", "gray", "cyan", "magenta", "yellow", " light, dark, gray, light, darkgrey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver "," teal. "

So basically you are using the wrong color format for the parseColor() method. Just delete one extra # and you will go well.

 footer.setBackgroundColor(Color.parseColor("#ffb5d6e1")); 

Bonus

For simplicity, you can also remove the opacity component from your color. You set the opacity to 100% using "ff", but this is the default behavior. This way you can simply remove this component and keep it simple, for example,

 footer.setBackgroundColor(Color.parseColor("#b5d6e1")); 
+1
source

Basically this is what you need to set the background color

 please follow the following steps 

Create a linear layout view, such as LinearLayout m = (LinearLayout) findViewByid (R.id.line1); m.setBackground (R.color.back);

0
source

Source: https://habr.com/ru/post/1438194/


All Articles