Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create drawable with colored background and centered icon

I'm trying to create a drawable painted by color with centered icon. I tried to create layer-list but it scales icon to match drawable size:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/colorImageHolder"/>
        </shape>
    </item>

    <item android:drawable="@drawable/no_photo" android:gravity="center">
    </item>
</layer-list>

This is a real device picture:

enter image description here

I want it to look like this:

enter image description here

Is there any way to disable icon scaling ?

UPDATE: My ImageView has scaleType="centerCrop" and I think this is a root of problem :( I tried to place no_photo drawable as background but it scales again.

like image 900
Alexandr Shutko Avatar asked Apr 06 '16 06:04

Alexandr Shutko


1 Answers

Try this

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/colorImageHolder"/>
        </shape>
    </item>

    <item android:drawable="@drawable/no_photo"
          android:bottom="20dp"
          android:left="20dp"
          android:right="20dp"
          android:top="20dp"
    >
    </item>
</layer-list>

Note: This solution only work with square or oval view, not rectangle

like image 109
Linh Avatar answered Nov 10 '22 00:11

Linh