Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: How to use custom colors?

Im trying to set the backgroundcolor of my FrameLayout using my own colors.

I created a .xml file which contains my own colors. Here it is:

<?xml version="1.0" encoding="utf-8"?> 
<resources>
    <color name="red">#FF0000</color>
    <color name="orange">#FF7D00</color>
    <color name="yellow">#FFFF00</color>
    <color name="green">#00FF00</color>
    <color name="blue">#00B4FF</color>
    <color name="black">#000000</color>
</resources>

And here is the code where Im trying to set the backgroundcolor, but when I run the app, the FrameLayout is always grey, why?:

FrameLayout MyFrameLayout = new FrameLayout(this);
LayoutParams MyFrameLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, 200);
MyFrameLayout.setLayoutParams(MyFrameLayoutParam);

MyFrameLayout.setBackgroundColor(R.color.red);

Parent.addView(MyFrameLayout);
like image 570
user1655806 Avatar asked Sep 07 '12 21:09

user1655806


People also ask

How do I get a custom color palette on my android?

On the “Wallpaper & style” page, select “Wallpaper colors” and choose one of the color combinations shown. You can also tap on “Basic colors” if you want something, well, basic.

How can we use custom color in application?

FrameLayout MyFrameLayout = new FrameLayout(this); LayoutParams MyFrameLayoutParam = new LayoutParams(LayoutParams. FILL_PARENT, 200); MyFrameLayout. setLayoutParams(MyFrameLayoutParam); MyFrameLayout. setBackgroundColor(R.


1 Answers

You need to retrieve the color from resources before setting it. You are assigning the R.java id not the actual value.

Color red = getApplicationContext().getResources().getColor(R.color.red)
MyFrameLayout.setBackgroundColor(red);
like image 59
tsmith Avatar answered Oct 02 '22 14:10

tsmith