Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

background image not repeating in android layout

Tags:

i've used the following code to repeat the image in the background but its not working can any one help?

Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@drawable/grass_bg"     > 

grass_bg.xml in drawable looks like this

<?xml version="1.0" encoding="utf-8"?>     <bitmap xmlns:android="http://schemas.android.com/apk/res/android"         android:src="@drawable/grass_small"         android:tileMode="repeat"/> 

its showing the same small image. its not repeating...

like image 410
sujith Avatar asked Nov 02 '10 12:11

sujith


People also ask

How would you set a background image to not repeat?

To make a background image not repeat in HTML, specify no-repeat in the background-repeat property or the background shorthand property. The background image is set to 'no-repeat' using the 'background-repeat' property.

How do I make my background-repeat vertically?

The background-repeat property sets if/how a background image will be repeated. By default, a background-image is repeated both vertically and horizontally. Tip: The background image is placed according to the background-position property.


2 Answers

Bitmaps (and their states) get reused a lot, and I've found it's easy to lose the tileMode if a BitmapDrawable is used in more than one place. The following code fixes the problem for me:

 public static void fixBackgroundRepeat(View view) {       Drawable bg = view.getBackground();       if(bg != null) {            if(bg instanceof BitmapDrawable) {                 BitmapDrawable bmp = (BitmapDrawable) bg;                 bmp.mutate(); // make sure that we aren't sharing state anymore                 bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);            }       }  } 
like image 177
Alec B. Plumb Avatar answered Oct 18 '22 20:10

Alec B. Plumb


Create a copy of grass_bg.xml for each time you use it (ie grass_bg_2.xml). This worked for me to assure the tileMode setting was not lost when the same background is used repeatedly.

like image 32
OldSchool4664 Avatar answered Oct 18 '22 19:10

OldSchool4664