ono
ono

Reputation: 3104

Drawable padding overrides layout padding

I have a TextView like so:

<TextView
   android:id="@+id/friends"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="8dp" />

And I'm setting the background like:

textView.setBackgroundResource(R.drawable.background_color);

where background_color.xml is:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <solid android:color="#217dd2" />
    </shape>
</item>
<item
    android:left="5dip">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <solid android:color="#deebf6" />
    </shape>
</item>

This removes my default padding of 8dp and replaces it with the left padding of 5dp that I have declared on the 2nd item of the layered-list. Any way to avoid this? Or another way to create a 2 color background for a View?

Upvotes: 3

Views: 1057

Answers (1)

Andrei Mankevich
Andrei Mankevich

Reputation: 2283

That's known issue, setBackgroundResource method resets paddings. Try to set your background from XML:

<TextView
   android:id="@+id/friends"
   android:background="@drawable/background_color"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="8dp" />

It will work properly in this case.

Upvotes: 2

Related Questions