Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where'd padding go, when setting background Drawable?

Tags:

android

I have this issue on my EditText and Button views, where I have a nice padding for them to space away from the text, but when I change the background with setBackgroundDrawable or setBackgroundResource that padding is lost forever.

like image 621
Dandre Allison Avatar asked Apr 10 '12 19:04

Dandre Allison


People also ask

How do you add drawable padding?

You can use android:drawableLeft="@drawable/your_icon" to set the drawable to be shown on the left side. In order to set a padding for the drawable you should use the android:paddingLeft or android:paddingRight to set the left/right padding respectively.

Which image format is preferred for storing local images in the drawable directory?

Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged). App icons, logos, and other graphics, such as those used in games, are well suited for this technique. To use an image resource, add your file to the res/drawable/ directory of your project.


1 Answers

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); 
like image 155
Matt McMinn Avatar answered Oct 16 '22 01:10

Matt McMinn