LayerDrawable programmatically
Asked Answered
K

2

12

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 shapes as shown below. But i want to do this programatically

How to build this same drawable programatically?

<item>
    <shape>
        <solid android:color="@android:color/yellow" />
    </shape>
</item>

<!-- main color -->
<item
    android:bottom="1dp"
    android:left="1dp"
    android:right="1dp">
    <shape>
        <solid android:color="@android:color/white" />
    </shape>
</item>

<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="10dp">
    <shape>
        <solid android:color="@android:color/white" />
    </shape>
</item>

this is what i tried....

    GradientDrawable border = new GradientDrawable();
    border.setShape(GradientDrawable.RECTANGLE);
    border.setColor(Color.WHITE);

    GradientDrawable background = new GradientDrawable();
    background.setShape(GradientDrawable.RECTANGLE);
    background.setColor(Color.YELLOW);


    GradientDrawable clip = new GradientDrawable();
    clip.setShape(GradientDrawable.RECTANGLE);
    border.setColor(Color.WHITE);

    Drawable[] layers = {background, border, clip};
    LayerDrawable layerDrawable = new LayerDrawable(layers);

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

but the result is different....please help....!

Kastner answered 21/11, 2013 at 12:13 Comment(3)
If your App-Theme is dynamic, why you don't use styles? developer.android.com/guide/topics/ui/themes.htmlJarret
I cannot keep values like colors in xml also... in my case color values are coming from server anyway thanks for your time... :)Kastner
Have you ever thought about using some images and applying a color filter on those images to implement the themes.Ibsen
K
35

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.

ShapeDrawable border = new ShapeDrawable();
border.getPaint().setColor(Color.WHITE);

ShapeDrawable background = new ShapeDrawable();
background.getPaint().setColor(Color.BLACK);


ShapeDrawable clip = new ShapeDrawable();
clip.getPaint().setColor(Color.WHITE);

Drawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);

layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);
Kastner answered 22/11, 2013 at 6:55 Comment(2)
More explanation would have been better.Escalade
There is one thing you missed. The setLayerInset() method accepts px, not dp.Rahal
M
0

This also works with Gradient Drawables:

GradientDrawable border = new GradientDrawable();
border.setColor(Color.White);

GradientDrawable background = new GradientDrawable();
background.setColor(Color.Black);

GradientDrawable clip = new GradientDrawable();
clip.setColor(Color.White);

GradientDrawable[] layers = {background, border, clip};
LayerDrawable layerDrawable = new LayerDrawable(layers);

layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 1);
layerDrawable.setLayerInset(2, 0, 0, 0, 10);
Murat answered 8/7, 2017 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.