Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use drawable+color as background?

Tags:

android

I have an opaque pattern (10% opacity) and I need to combine it with some color.

Is it possible to set background color to image?

like image 939
Dmitry Zaytsev Avatar asked Jul 22 '12 09:07

Dmitry Zaytsev


People also ask

Is a drawable to use as the background?

Create a resource in drawable (like files_bg. xml) folder and set it as background for layout. Use two item in layer list, one for solid color in shape and other one bitmap. and now set drawable as background in layout or wherever you are using it.

How do I change the color of a drawable in xml?

Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor" .


1 Answers

Sure. Just use a LayerListDrawable.

Something like:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#FF00ffff" />
        </shape>
    </item>
    <item>
         <bitmap 
             android:src="@drawable/your_tiled_drawable"
             android:tileMode="repeat" />
    </item>
</layer-list>

Save that in your res/drawables directory, and use it as your background.

Sadly, if your application is more complicated, sometimes the tileMode will get unset for no apparent reason. If you run in to that, you'll have to re-add the tile mode in Java.

I usually still set it in xml, and then do something like:

View v = findViewById(R.id.my_view);
LayerDrawable lld = (LayerDrawable) v.getBackground();
BitmapDrawable tiled = (BitmapDrawable) lld.getDrawable(1);
tiled.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
like image 186
ABentSpoon Avatar answered Sep 19 '22 18:09

ABentSpoon