Wednesday, November 11, 2015

android - How to create LayerDrawable programmatically

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#e8f7e8"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply Multi-Color Border"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Multi-Color Border"
        android:textSize="50dp"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mBTN;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mBTN = (Button) findViewById(R.id.btn);

        // Set a click listener for Button widget
        mBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    ColorDrawable
                        A specialized Drawable that fills the Canvas with a specified color. Note
                        that a ColorDrawable ignores the ColorFilter.

                        It can be defined in an XML file with the <color> element.
                */
                /*
                    ColorDrawable(int color)
                        Creates a new ColorDrawable with the specified color.
                */
                // Initialize some new ColorDrawable objects
                ColorDrawable leftBorder = new ColorDrawable(Color.RED);
                ColorDrawable topBorder = new ColorDrawable(Color.GREEN);
                ColorDrawable rightBorder = new ColorDrawable(Color.BLUE);
                ColorDrawable bottomBorder = new ColorDrawable(Color.YELLOW);
                ColorDrawable background = new ColorDrawable(Color.WHITE);

                // Initialize an array of Drawable objects
                Drawable[] layers = new Drawable[]{
                        leftBorder, // Red color
                        topBorder, // Green color
                        rightBorder, // Blue color
                        bottomBorder, // Yellow color
                        background // White background
                };

                /*
                    LayerDrawable
                        A Drawable that manages an array of other Drawables. These are drawn in
                        array order, so the element with the largest index will be drawn on top.

                        It can be defined in an XML file with the <layer-list> element. Each
                        Drawable in the layer is defined in a nested <item>.
                */
                /*
                    public LayerDrawable (Drawable[] layers)
                        Creates a new layer drawable with the list of specified layers.

                    Parameters
                        layers : a list of drawables to use as layers in this new drawable,
                            must be non-null
                */
                /*
                    Drawable
                        A Drawable is a general abstraction for "something that can be drawn.
                        " Most often you will deal with Drawable as the type of resource retrieved
                        for drawing things to the screen; the Drawable class provides a generic API
                        for dealing with an underlying visual resource that may take a variety of
                        forms. Unlike a View, a Drawable does not have any facility to receive
                        events or otherwise interact with the user.
                */
                // Initialize a new LayerDrawable
                LayerDrawable layerDrawable = new LayerDrawable(layers);

                /*
                    public void setLayerInset (int index, int l, int t, int r, int b)
                        Specifies the insets in pixels for the drawable at the specified index.

                    Related XML Attributes
                        android:left
                        android:top
                        android:right
                        android:bottom
                    Parameters
                        index : the index of the drawable to adjust
                        l : number of pixels to add to the left bound
                        t : number of pixels to add to the top bound
                        r : number of pixels to subtract from the right bound
                        b : number of pixels to subtract from the bottom bound
                */
                // Red layer padding, draw left border
                layerDrawable.setLayerInset(0,0,0,15,0);

                // Green layer padding, draw top border
                layerDrawable.setLayerInset(1,15,0,0,15);

                // Blue layer padding, draw right border
                layerDrawable.setLayerInset(2,15,15,0,0);

                // Yellow layer padding, draw bottom border
                layerDrawable.setLayerInset(3,15,15,15,0);

                // White layer, draw the background
                layerDrawable.setLayerInset(4,15,15,15,15);

                /*
                    Finally, Set the LayerDrawable as TextView background.
                    This will draw multi-color border and white background.
                    Draw 15 pixels width border
                */
                mTextView.setBackground(layerDrawable);

                // Set the TextView padding
                mTextView.setPadding(25,25,25,25);
            }
        });
    }
}
More android examples