I searched a lot but didn't find anything. I want to make a background for a layout , but instead of filling it with a color or a gradient I want to fill it with a texture and repeat it . I tried this but it didn't work . Can you tell me a solution.
card_texture.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/paper_texture"
android:tileMode="repeat"
/>
card_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color = "@android:color/transparent"/>
<stroke android:width="2dp"
android:color="@color/text_color"/>
<corners
android:radius="20dp"
/>
</shape>
card_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/card_texture" >
<corners
android:radius = "20dp"/>
</item>
<item android:drawable="@drawable/card_shape" />
</layer-list>
and this is how I use it in my Layout android:background="@drawable/card_background"
I get something like this. But I want the corners(marked with red) of the texture to be transparent and to fit in the black border
I called this res/drawable/bg_tile.xml
I use it like:
Obiously, I prepared a tile to be mirrored horizontally and vertically (most tiles make a funny effect like that) and called it res/drawable-a-dpi-resolution/tile.png
a-dpi-resolution being ldpi, mdpi, ..., xxxhdpi
If you want to use a composite background (i.e. a gradient and a tiling bitmap over it - but the bitmap has to be somehow transparent, or it will cover the gradient), you can make a LayerList background (so that different "layers" are dran one ontop of another)
To do that, you'll need to add another background file, i.e.: res/drawable/bg_radial.xml:
and finally the LayerList, which sticks them together (res/drawable/bg.xml):
Note that the first item is drawn first. Next items are drawn ontop of each other, being the last one the "uppermost".
So, finally you would use this like:
Nice!
to make your image corners rounded use following function:
in
onCreate
do this: