Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.util.TypedValue; import android.view.View; public class Main { public static final int TRANSPARENT = 0; /** * Set the background of a {@link android.view.View} to the specified color, with a darker version of the * color as a border. */ public static void setViewBackground(View view, int color) { Resources r = view.getResources(); Drawable currentDrawable = view.getBackground(); GradientDrawable backgroundDrawable; if (currentDrawable != null && currentDrawable instanceof GradientDrawable) { // Reuse drawable backgroundDrawable = (GradientDrawable) currentDrawable; } else { backgroundDrawable = new GradientDrawable(); } int darkenedColor = darkenColor(color); backgroundDrawable.setColor(color); backgroundDrawable.setStroke( (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics()), darkenedColor); view.setBackgroundDrawable(backgroundDrawable); } /** * Returns a darker version of the specified color. Returns a translucent gray for transparent colors. */ private static int darkenColor(int color) { if (color == TRANSPARENT) { return Color.argb(127, 127, 127, 127); } return Color.rgb(Color.red(color) * 192 / 256, Color.green(color) * 192 / 256, Color.blue(color) * 192 / 256); } }