Where’d padding go, when setting background Drawable?

What I found was adding a 9 patch as a background resource reset the padding – although interestingly if I added a color, or non-9 patch image, it didn’t. The solution was to save the padding values before the background gets added, then set them again afterwards.

private EditText value = (EditText) findViewById(R.id.value);

int pL = value.getPaddingLeft();
int pT = value.getPaddingTop();
int pR = value.getPaddingRight();
int pB = value.getPaddingBottom();

value.setBackgroundResource(R.drawable.bkg);
value.setPadding(pL, pT, pR, pB);

Leave a Comment