Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GradientDrawable in Code

Tags:

android

I can use the following XML just fine

<shape android:shape="rectangle" xmlns...">
     <gradient
         android:startColor="#255779"
         android:centerColor="#3e7492"
         android:endColor="#a6c0cd"
         android:angle="90"/>

    <stroke android:width="1dp" android:color="#0d202e"/>
</shape>

the gradient come up nicely

i am trying to do the same thing just using code (no XMLs)

int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };

GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);

setBackgroundDrawable(g);

The gradient DOES come up but its not the same as the one the one from XML, i mean the colors are same but gradient is not same, i think it has to do with the start,middle,end colors in the xml

also how do i add a stroke

any help will be greatly appreciated

like image 381
Monty Avatar asked Nov 14 '10 12:11

Monty


People also ask

What is Android GradientDrawable?

A Drawable with a color gradient for buttons, backgrounds, etc. It can be defined in an XML file with the <shape> element.

What is gradients in android?

Designers are trying out different styles and combinations which go best with the Android App. One of the key components of Android being used these days is called GradientDrawable. A GradientDrawable is drawable with a color gradient for buttons, backgrounds, etc.

How do you color a gradient in XML?

To create a gradient color we need to create a . xml file in the drawable folder. So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable. xml file.


1 Answers

According to Docs

android:angle
Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0


but you are using GradientDrawable.Orientation.TOP_BOTTOM in your code. That's why gradient directions are different

You should use GradientDrawable.Orientation.BOTTOM_TOP instead.

like image 141
woodshy Avatar answered Sep 18 '22 09:09

woodshy