Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.drawable.Drawable; import android.os.Build; import android.view.View; public class Main { public static void setBackgroundKeepingPadding(View view, Drawable drawable) { int top = view.getPaddingTop(); int left = view.getPaddingLeft(); int right = view.getPaddingRight(); int bottom = view.getPaddingBottom(); setBackground(view, drawable); view.setPadding(left, top, right, bottom); } public static void setBackground(View view, Drawable drawable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(drawable); } else { view.setBackgroundDrawable(drawable); } } }