Android: how to create a background from pattern?

AndroidBackgroundDesign Patterns

Android Problem Overview


I have a pattern (.png image 4x4px) and have to fill the layout with it.

Does anyone know how to do this?

If I simply select the drawable as a background the image, it is stretched; instead it needs to be repeated along the x and y axis.

Android Solutions


Solution 1 - Android

Here is a really nice explanation:

Put your "back.png" image on "drawable" folder. Then create a drawable "backrepeat.xml" like that:

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

In your layout, add android:background="@drawable/backrepeat":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/MainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backrepeat">

</LinearLayout>

As is the case with many Android good practices/handy tricks, it can be traced back to Romain Guy.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiko GamulinView Question on Stackoverflow
Solution 1 - AndroidDimitar DimitrovView Answer on Stackoverflow