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
A Drawable with a color gradient for buttons, backgrounds, etc. It can be defined in an XML file with the <shape> element.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With