Have you ever gushed over beautiful , very beautiful UI of your android apps? Sorry, most of the times these are iphone apps. That does not mean you can try to create beautiful UIs in android app.
You can create a gradient background to your button of layout using xml easily. But today let us consider how it is done using code.
This is how the button would look.
The code is almost self explanatory. The constructor of GradientDrawable takes the orientation of gradient and array of colors as parameter. Orientation can be top to bottom, bottom to top, left to right etc, or TL_BR or TR_BL (top left to bottom right - diagonal and top right to bottom left). Unlike xml, the colors can be more than 3. Stroke is the line surrounding the gradient with first parameter as thickness of the line.
If you are more creative, you need to restrict yourself with java colors, you can even use html color codes.
e.g.
The parseColor function will convert html color code to java int color. Its string parameter is the code in the form #RRGGBB. If you provide improper code, it throws invalid argument exception at runtime.
Instead of linear gradient, you can have radial gradient or sweep gradient. The shape of gradient can be rectangle, line, oval or ring.
To use radial or radial gradient, you have to mention center of gradient and the radius. The center 1,1 is taken as lower right corner, 0,0 as upper left corner of your widget. Look at this radial gradient
And the code for this radial gradient shown in the begining of post is
int colors[]= new int[]{Color.parseColor("#cff2f5"), Color.parseColor("#23c5ca"),Color.parseColor("#21b1b9")};
GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
grDr.setGradientType(GradientDrawable.RADIAL_GRADIENT);
grDr.setGradientCenter(0,0.5f);
grDr.setGradientRadius(600);
By setting gradient type to sweeping, (and you need not set radius or center), you can create even more colorful gradients as shown below
int colors2[] = new int[]{Color.BLUE,Color.YELLOW,Color.RED,Color.GREEN,Color.MAGENTA,Color.WHITE};
GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
In the next post, we will see how to use layer drawable.
You can create a gradient background to your button of layout using xml easily. But today let us consider how it is done using code.
int colors2[] = new int[]{Color.CYAN, Color.WHITE,Color.CYAN};
GradientDrawable grDr =new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors2);
grDr.setCornerRadius(5);
grDr.setGradientType(GradientDrawable,LINEAR_GRADIENT);
grDr.setStroke(1,Color.WHITE);
btn.setBackgroundDrawable(grDr);
btn.setBackgroundDrawable(grDr);
This is how the button would look.
The code is almost self explanatory. The constructor of GradientDrawable takes the orientation of gradient and array of colors as parameter. Orientation can be top to bottom, bottom to top, left to right etc, or TL_BR or TR_BL (top left to bottom right - diagonal and top right to bottom left). Unlike xml, the colors can be more than 3. Stroke is the line surrounding the gradient with first parameter as thickness of the line.
If you are more creative, you need to restrict yourself with java colors, you can even use html color codes.
e.g.
int
colors[] = new int[]{Color.parseColor("#451d42"),Color.parseColor("#efa5bc") ,
Color.parseColor("#b67caf"),Color.parseColor("#1e3e65"),Color.parseColor("#3224af")};
The parseColor function will convert html color code to java int color. Its string parameter is the code in the form #RRGGBB. If you provide improper code, it throws invalid argument exception at runtime.
Instead of linear gradient, you can have radial gradient or sweep gradient. The shape of gradient can be rectangle, line, oval or ring.
To use radial or radial gradient, you have to mention center of gradient and the radius. The center 1,1 is taken as lower right corner, 0,0 as upper left corner of your widget. Look at this radial gradient
And the code for this radial gradient shown in the begining of post is
int colors[]= new int[]{Color.parseColor("#cff2f5"), Color.parseColor("#23c5ca"),Color.parseColor("#21b1b9")};
GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
grDr.setGradientType(GradientDrawable.RADIAL_GRADIENT);
grDr.setGradientCenter(0,0.5f);
grDr.setGradientRadius(600);
By setting gradient type to sweeping, (and you need not set radius or center), you can create even more colorful gradients as shown below
int colors2[] = new int[]{Color.BLUE,Color.YELLOW,Color.RED,Color.GREEN,Color.MAGENTA,Color.WHITE};
GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
grDr.setGradientType(GradientDrawable.RADIAL_GRADIENT);
In the next post, we will see how to use layer drawable.
Comments
Post a Comment