Skip to content Skip to sidebar Skip to footer

Layerdrawable Programmatically

Since my application's color theme is dynamic i can only create background drawables using colors and shapedrawables, i want to build a edittext background drawable with colors and

Solution 1:

I finally got it working. Instead of using GradientDrawable I used ShapeDrawable.

By setting this LayerDrawable as an EditText background you can regenerate default EditText styles with custom colors.

ShapeDrawableborder=newShapeDrawable();
border.getPaint().setColor(Color.WHITE);

ShapeDrawablebackground=newShapeDrawable();
background.getPaint().setColor(Color.BLACK);


ShapeDrawableclip=newShapeDrawable();
clip.getPaint().setColor(Color.WHITE);

Drawable[] layers = {background, border, clip};
LayerDrawablelayerDrawable=newLayerDrawable(layers);

layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);

Solution 2:

This also works with Gradient Drawables:

GradientDrawableborder=newGradientDrawable();
border.setColor(Color.White);

GradientDrawablebackground=newGradientDrawable();
background.setColor(Color.Black);

GradientDrawableclip=newGradientDrawable();
clip.setColor(Color.White);

GradientDrawable[] layers = {background, border, clip};
LayerDrawablelayerDrawable=newLayerDrawable(layers);

layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);

Post a Comment for "Layerdrawable Programmatically"