24

I have the following LinearLayout. What I don't understand is if I set the background to another image, the padding information are reset. Is there a way to prevent this?

<LinearLayout android:id="@+id/aPanel"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="@drawable/bkground"
    android:paddingLeft="15dp" android:paddingRight="15dp"> 

     <!-- some children here -->
     </LinearLayout>

I see the position of the children get shifted when I change the background drawable of the LinearLayout aPanel.

user
  • 85,380
  • 17
  • 189
  • 186
michael
  • 93,094
  • 111
  • 230
  • 334
  • What is your evidence that "the padding information are reset"? In other words, what are your actual symptoms? – CommonsWare May 22 '10 at 15:08
  • 2
    Same problem as http://stackoverflow.com/questions/5890379/android-setbackgroundresource-discards-my-xml-layout-attributes - the padding simply gets reset after setBackgroundResource() is called (possibly related to only 9-patch backgrounds). – Artem Russakovskii Aug 29 '11 at 22:39

3 Answers3

43

This is the default behavior when changing the background Drawable on a View. According to Romain Guy, one of the Android developers, "The reason why setting an image resets the padding is because 9-patch images can encode padding." See his full answer in a similar question.

The fix is to reset the padding in code each time you change the background drawable.

happydude
  • 3,818
  • 1
  • 21
  • 41
20

This is a similar question.

in short, the answer would be :

public static void setViewBackgroundWithoutResettingPadding(final View v, final int backgroundResId) {
    final int paddingBottom = v.getPaddingBottom(), paddingLeft = v.getPaddingLeft();
    final int paddingRight = v.getPaddingRight(), paddingTop = v.getPaddingTop();
    v.setBackgroundResource(backgroundResId);
    v.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

The reason for the padding being reset is because the drawable might be a 9-patch drawable .

Community
  • 1
  • 1
android developer
  • 106,412
  • 122
  • 641
  • 1,128
  • 1
    You can also set background using selector with proper set of states. Then you don't have to restore paddings – Arkadiusz Cieśliński Feb 06 '15 at 16:28
  • @ArkadiuszCieśliński Sure. depends on the case and your needs – android developer Feb 06 '15 at 22:22
  • @androiddeveloper I had to post a runnable to the view to make the padding work, why is this happening? – zero.zero.seven Jun 26 '15 at 11:11
  • @zero.zero.seven I don't remember such an issue. Maybe you should post about it, and show some code to give clues about the reason for this. Maybe it's also best to create a tiny app sample to show your issue. – android developer Jun 26 '15 at 11:22
  • @androiddeveloper if the following information didn't help, I will create a thread about it, I do the following operations on the TextView: set backround, set text, set padding on four sides, and set typeface; and the bottom/right padding get nasty sometimes, I tried the runnable, so the issue happens less! but it's still out there on some items of the listview – zero.zero.seven Jun 26 '15 at 11:38
  • @zero.zero.seven Sounds odd. Have you tried using a 9-patch , if it's even possible on your case? – android developer Jun 26 '15 at 12:16
4

You can use shape to set the background with single colour or image and apply padding like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners
        android:bottomLeftRadius="8dip"
        android:topLeftRadius="8dip"/>
    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip"/>
</shape>
SpyZip
  • 5,294
  • 4
  • 29
  • 51