Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient XML drawable center not working

I am trying to create an XML gradient drawable to match this style mentioned in Material Design guidelines

Gradient guideline

This is my XML code:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:centerY="30%"
        android:endColor="@android:color/transparent"
        android:startColor="#4d000000"
        android:type="linear"/>
</shape>

I already tried to set the centerY to 0.3 too, without success. The drawable always look the same in preview.

Can someone tell me what to do to achieve that style? Thanks in advance.

like image 790
Jahir Fiquitiva Avatar asked Aug 19 '16 21:08

Jahir Fiquitiva


2 Answers

centerX/centerY don't work without setting centerColor.

like image 185
miguel Avatar answered Nov 20 '22 11:11

miguel


I don't quite see your mistake. It could be possible that you first have to set the centerColor. Take a look at the docs if you haven't already:

https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

But before hustling with a XML drawable that will give you a linear gradient, I suggest you to use Roman Nuriks attempt to this problem. See more here: https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf He solved the problem by using a Java implementation of a Drawable. Works as any other drawable.

like image 6
Jonas Drotleff Avatar answered Nov 20 '22 12:11

Jonas Drotleff