Setting background colour of Android layout element
Solution 1
You can use simple color resources, specified usually inside res/values/colors.xml
.
<color name="red">#ffff0000</color>
and use this via android:background="@color/red"
. This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red)
.
You can also use any drawable resource as a background, use android:background="@drawable/mydrawable"
for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
Solution 2
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id
line in res/layout/*.xml
<RelativeLayout ...
...
android:id="@+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
Solution 3
You can use android:background="#DC143C"
, or any other RGB values for your color. I have no problem using it this way, as stated here
Solution 4
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="@color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
Solution 5
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="@android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
Related videos on Youtube
Bjarke Freund-Hansen
Updated on February 04, 2020Comments
-
Bjarke Freund-Hansen over 4 years
I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a
TextView
in aRelativeLayout
. Now I wish to change the background colour of theRelativeLayout
, however I cannot seem to figure out how.I know I can set the
android:background
property in theRelativeLayout
tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it adrawable
or astring
?Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
-
lucas over 10 yearswhat software did you use to draw the graph on the right side?
-
Bjarke Freund-Hansen over 10 years@lucas: I did not draw the diagrams, as I noted in the questions, it is from a set of slides on Android UI design. See the link in the question.
-
-
Bjarke Freund-Hansen almost 13 yearsWorks like a charm, thanks. Could you point me to the reference where I should have read this?
-
Admin almost 13 yearsUhh actually: No. Just searched the docs, this is pretty standard android stuff, but seems nowhere really documented. Neither the tutorials on the dev site nor the api samples make use of this. The android doc is somewhat lacking when it comes to some features. I think I picked it up by accident in some external tutorials. Usually it's a good idea to browse the api samples and sample projects though. You can find the code inside the
ANDROID_SDK\samples
folder (for various android versions). The whole api sample app comes also preinstalled in every emulator instance. -
Admin almost 13 yearsAlso just checked the UI designer. Nothing easy to be found. But I recommend writing things by hand in the xml anyway. The designer improved a lot recently, but it's still not useable in my opinion. Not only are some options limited, the layout sometimes looks completely different on a real device (especially when using referenced drawable resources. They don't get scaled correctly or are even not displayed at all in my experience). Test your layouts on your device or on an emulator.
-
Bjarke Freund-Hansen about 11 years-1 because I explicitly wrote "I want to define a new colour that I can use in multiple places" in the question, because I did not want to hardcode the color value, but define it as a resource I can use in several places.
-
elimirks almost 11 yearsDid you forget to wrap the color tag with a resources tag?
-
Guillermo Gutiérrez almost 11 yearsIf you want it dynamic, I think you cannot use XML.
-
Zac about 10 years+1 for I need to change it at runtime according to status flag; I was also able to get original color back by using the Color.TRANSPARENT constant.
-
dalf over 9 yearsfor some "default" colors, you can use this syntax: android:background="@android:color/white"
-
Anubian Noob almost 9 years@BjarkeFreund-Hansen He acknowledges the other answers and provides this programmatic solution. Not downvote worthy.
-
Bjarke Freund-Hansen almost 9 years@AnubianNoob: I disagree. The question is on how to define a colour in markup, and the original answer was how to set a static hard-coded color value. So the answer specifically did not answer the question. With the latest edit to the answer, it is more relevant, but that was made after my comment.
-
Bjarke Freund-Hansen almost 9 years@GMsoF: Oh it does work but it does not answer the question.
-
Android Killer almost 9 years@BjarkeFreund-Hansen, the question is how to set the background not how to define color, read properly. And so what if the question is on that. I acknowledge that and told "you can also go like"....so he may go or may not. Also it may help others as it already did you can see. can you ??
-
Bjarke Freund-Hansen almost 9 years@AndroidKiller: Question specifically states: "I want to define a new colour that I can use in multiple places.". The problem I see is that this answer (along with several of the others) confuses the reader, in that it is very specifically not an answer to what is being asked. I bet there are several other questions where this is the perfect answer, so it should be posted there instead. This is becoming a meta discussion, and if you really believe my -1 vote is a general problem that should be discussed, then please create a meta question and I will answer there.
-
ToolmakerSteve over 8 yearsYou can use the color string directly in the layout element:
android:background="#f00"
(or any of the other color string formats with more digits). -
Rahul over 8 yearsplus one just to compensate the minus one from @BjarkeFreund-Hansen
-
Chit Khine about 8 yearsif i want to change list rows color randomly we can use it, thanks
-
Rohit Bandil over 7 yearsgetResources().getColor() is deprecated now.
-
maracuja-juice about 7 years@RohitBandil What should we use now?
-
Ganpat Kaliya about 7 yearsHello, I set the background color of screen to blue. But some times it display black. why this happen ? please help me.