1

i try to draw LinearGradient in a layout but the gradient is not fit into my view.

Instead of gradient i see only one color.

I think its because im not giving the right dimension of the view

Here is my code:

backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);

int[] tempColors = data.getAppBackgroundColor();

        LinearGradient test = new LinearGradient(0.f, 0.f, backGroundColorView.getWidth(), backGroundColorView.getHeight(), tempColors, null, TileMode.CLAMP);

        ShapeDrawable shape = new ShapeDrawable(new RectShape());
        shape.getPaint().setShader(test);

        backGroundColorView.setBackgroundDrawable(shape);

Thank for helping

dasdasd
  • 1,971
  • 10
  • 45
  • 72

4 Answers4

1

You can use this site do create the gradient you want and then add a file in your res/drawable and put the code inside.

After that all you have to do is set the background of your layout to be the drawable file you just created.

If you have any questions just ask ;)

EDIT: Change your code to this:

 GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,tempColors);
 gd.setCornerRadius(0f);

 backGroundColorView.setBackgroundDrawable(gd);
Dyna
  • 2,304
  • 1
  • 14
  • 25
1

just use GradientDrawable as a background http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

pskink
  • 23,874
  • 6
  • 66
  • 77
1

Try this

backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);
int[] tempColors = data.getAppBackgroundColor();

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,tempColors);
gd.setCornerRadius(0f);

backGroundColorView .setBackgroundDrawable(gd);

Instead of

backGroundColorView = (LinearLayout) findViewById(R.id.backGroundColorView);

int[] tempColors = data.getAppBackgroundColor();

        LinearGradient test = new LinearGradient(0.f, 0.f, backGroundColorView.getWidth(), backGroundColorView.getHeight(), tempColors, null, TileMode.CLAMP);

        ShapeDrawable shape = new ShapeDrawable(new RectShape());
        shape.getPaint().setShader(test);

        backGroundColorView.setBackgroundDrawable(shape);

GradientDrawable

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
0

you can create linear gradiant like this:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
android:type="linear"
android:centerX="50%" 
android:startColor="#FF003333" 
android:centerColor="#FF05C1FF" 
android:endColor="#FF003333" 
android:angle="270"/>
<!-- <gradient
    android:centerColor="#FF05C1FF"
    android:centerX="50%"
    android:centerY="50%"
    android:endColor="#FF003333"
    android:gradientRadius="50"
    android:startColor="#FF003333"
    android:type="radial" /> -->

<corners
    android:bottomLeftRadius="15dp"
    android:bottomRightRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp" />

</shape>

and use it as background of linear layout. ihope it solves your problem. see i have used this in my post: customToast

you can use this also:

GradientDrawable  grad = new GradientDrawable(Orientation.LEFT_RIGHT,
        new int[]{0xffffffff, 0xffff00ff, 0xffffff00, 
        0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);
Hamad
  • 5,096
  • 13
  • 37
  • 65