Java tutorial
//package com.java2s; // Use of this source code is governed by a BSD-style license that can be import android.support.annotation.DrawableRes; import android.view.View; public class Main { /** * Sets the background of a view to the given 9-patch resource and restores its padding. This * works around a bug in Android where the padding is lost when a 9-patch resource is applied * programmatically. */ public static void setNinePatchBackgroundResource(View view, @DrawableRes int resource) { int left = view.getPaddingLeft(); int top = view.getPaddingTop(); int right = view.getPaddingRight(); int bottom = view.getPaddingBottom(); view.setBackgroundResource(resource); view.setPadding(left, top, right, bottom); } }