Skip to content

Instantly share code, notes, and snippets.

@ptsiogas
Created October 1, 2015 13:56
Show Gist options
  • Save ptsiogas/e11b29a4324a58cb4d41 to your computer and use it in GitHub Desktop.
Save ptsiogas/e11b29a4324a58cb4d41 to your computer and use it in GitHub Desktop.
Create shadow effect background color programmatically.
/**
* Creates background color with shadow effect - programmatically
*
* @param color the background color
*/
private LayerDrawable setLayerShadow(String color) {
GradientDrawable shadow;
int strokeValue = 6;
int radiousValue = 2;
try{
int[] colors1 = {Color.parseColor(color), Color.parseColor("#FFFFFF")};
shadow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
shadow.setCornerRadius(radiousValue);
}
catch(Exception e){
int[] colors1 = {Color.parseColor("#419ED9"), Color.parseColor("#419ED9")};
shadow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
shadow.setCornerRadius(radiousValue);
e.printStackTrace();
}
int[] colors = {Color.parseColor("#D8D8D8"), Color.parseColor("#E5E3E4")};
GradientDrawable backColor = new GradientDrawable(GradientDrawable.Orientation.BL_TR, colors);
backColor.setCornerRadius(radiousValue);
backColor.setStroke(strokeValue, Color.parseColor("#D8D8D8"));
//finally c.reate a layer list and set them as background.
Drawable[] layers = new Drawable[2];
layers[0] = backColor;
layers[1] = shadow;
LayerDrawable layerList = new LayerDrawable(layers);
layerList.setLayerInset(0, 0, 0, 0, 0);
layerList.setLayerInset(1, strokeValue, strokeValue, strokeValue, strokeValue);
return layerList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment