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
This question is related to
java
android
colors
background
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)
Try this:
li.setBackgroundColor(android.R.color.red); //or which ever color do you want
EDIT: Posting logcat file would also help.
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"
Source: Stackoverflow.com