Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
ono Avatar asked Jan 21 '26 07:01

ono


1 Answers

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.

like image 60
Andrei Mankevich Avatar answered Jan 22 '26 21:01

Andrei Mankevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!