apply Border Drawable To View - Android Graphics

Android examples for Graphics:Drawable

Description

apply Border Drawable To View

Demo Code


//package com.java2s;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;

import android.view.View;

public class Main {
    public static void applyBorderDrawableToView(View v,
            int backgroundColor, int borderColor, int cornerRadius,
            int borderThickness) {
        Drawable drawable;/*from   w w  w.  jav a2 s .c  om*/
        drawable = createDrawableBackground(backgroundColor, cornerRadius,
                true, borderThickness, borderColor);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            v.setBackgroundDrawable(drawable);
        } else {
            v.setBackground(drawable);
        }
    }

    public static Drawable createDrawableBackground(int color,
            int cornerRadius, boolean hasBorder, int borderThickness,
            int borderColor) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        if (hasBorder)
            drawable.setStroke(borderThickness, borderColor);
        drawable.setCornerRadius(cornerRadius);
        drawable.setColor(color);
        return drawable;
    }
}

Related Tutorials