Using GradientDrawable as background in Sketchware Android Project

The public class GradientDrawable can be used to create a Drawable image with gradient of several colors which can be set as the background of any View.


The codes provided below shows how to use GradientDrawable in Sketchware Android Project. The codes provided below should be added in onCreate event, in add source directly block.

1. To set a single color as background of any View, use following code:
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setColor(Color.parseColor("#ff000000"));
linear1.setBackground(gd);

Or

android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setColor(Color.WHITE);
linear1.setBackground(gd);

Here gd is a GradientDrawable.
Here hex color code can be use to set background color.
And here gd is set as background of linear1. It can also be set as background of any other View (like button1, etc.).

2. To create a GradientDrawable with rounded corners use following code:
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setColor(Color.parseColor("#ff000000"));
gd.setCornerRadius(80);
linear1.setBackground(gd);

The extent to which it is rounded can be changed by increasing or decreasing the radius (set as 80 in above code).

3. To set a gradient of colors as GradientDrawable, define an array of colors as int[] and set them as it's color.
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
int clr[] = new int[]{ Color.parseColor("#ff0000"), Color.parseColor("#ffff06"), Color.parseColor("#dd00ff"), Color.parseColor("#ddff46") };
gd.setColors(clr);
gd.setCornerRadius(80);
linear1.setBackground(gd);


4. To change orientation of GradientDrawable add following code to the code above:
gd.setOrientation(android.graphics.drawable.GradientDrawable.Orientation.BL_TR);

The code above is for orientation from bottom left to top right.

Other orientation which can be used are BOTTOM_TOP, BR_TR, LEFT_RIGHT, RIGHT_LEFT, TL_BR, TOP_BOTTOM, TR_BL.


5. To add stroke or margin of particular width and color to the GradientDrawable, add following code to the code above:
gd.setStroke(8, Color.BLUE);

Here 8 is stroke width and Color.BLUE is color of stroke. The hex color code can be used by replacing color code with Color.parseColor("#ffrrggbb").

6. The shape of GradientDrawable can be made Oval using following code:
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
int clr[] = new int[]{ Color.parseColor("#ff0000"), Color.parseColor("#ffff06"), Color.parseColor("#dd00ff"), Color.parseColor("#ddff46") };
gd.setColors(clr);
gd.setShape(android.graphics.drawable.GradientDrawable.OVAL);
linear1.setBackground(gd);

7. The gradient type of GradientDrawable can be made Sweep type by using following code:
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
int clr[] = new int[]{ Color.parseColor("#ff0000"), Color.parseColor("#ffff06"), Color.parseColor("#dd00ff"), Color.parseColor("#ddff46") };
gd.setColors(clr);
gd.setGradientType(android.graphics.drawable.GradientDrawable.SWEEP_GRADIENT);
gd.setShape(android.graphics.drawable.GradientDrawable.OVAL);
linear1.setBackground(gd);

8. The gradient type can be made radial using following code:
gd.setGradientType(android.graphics.drawable.GradientDrawable.RADIAL_GRADIENT);

While using RADIAL_GRADIENT it is important to set GradientRadius as well. So the code for GradientDrawable of RADIAL_GRADIENT type should look like:
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
int clr[] = new int[]{ Color.parseColor("#ff0000"), Color.parseColor("#ffff06"), Color.parseColor("#dd00ff"), Color.parseColor("#ddff46") };
gd.setColors(clr);
gd.setGradientType(android.graphics.drawable.GradientDrawable.RADIAL_GRADIENT);
gd.setGradientRadius(500);
gd.setShape(android.graphics.drawable.GradientDrawable.OVAL);
linear1.setBackground(gd);



9. The alpha of GradientDrawable can be changed by adding following code:
gd.setAlpha(0.5);

10. The center of GradientDrawable can be changed using code:
gd.setGradientCenter(60, 80);

Here 60 is X coordinate and 80 is Y coordinate of center point of GradientDrawable.

Comments

  1. I am waiting for your 'Wordpress RSS xml feed parsing' video

    ReplyDelete
  2. Can you also make a background of app NOT FROM LINEAR

    ReplyDelete
    Replies
    1. How can I do that? Please help I'm new to sketchware 😬❤

      Delete
  3. Make background image by file if linear

    ReplyDelete
  4. How to set a background for textview sent
    Just like facebook post??

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware