Padding doesn't work with certain background resource

android, android-view

Solution

The call to:

this.setBackgroundResource(android.R.drawable.list_selector_background);

will remove any previosly set padding (this is to make it work properly with 9-patch assets).

Try to set the padding in code after the line above, like this:

this.setPadding(PADDING_CONSTANT, PADDING_CONSTANT, PADDING_CONSTANT, PADDING_CONSTANT);

Just remember that the values sent to setPadding is in pixels NOT dip!

Problem

Can anyone explain to me why this is happening? I have a fairly simple class extending TextView. When I set the background colour to Color.BLUE, padding works fine. When I change the background resource to android.R.drawable.list_selector_background, my padding is no longer applied. What the F? Here is my UI class: ``` public class GhostDropDownOption extends TextView { TextView text_view; public GhostDropDownOption(Context context, AttributeSet attrs) { super(context, attrs); setup(context); } public GhostDropDownOption(Context context) { super(context); setup(context); } private void setup(Context context) { this.setClickable(false); // THE 2 LINES BELOW ARE THE ONLY THING I'M CHANGING //this.setBackgroundResource(android.R.drawable.list_selector_background); this.setBackgroundColor(Color.BLUE); } } ``` And I'm using it in the layout like this: ``` <trioro.voyeur.ui.GhostDropDownOption android:id="@+id/tv_dropdown_option_1" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" android:text="@string/request_control_dropdown_option_1" android:textColor="#000000" android:padding="10dip"/> ``` And this is the result of changing the background:

Original source

Related problems