Centering a background image in Android

NotACleverMan picture NotACleverMan · Oct 3, 2010 · Viewed 21.4k times · Source

I have a background image about 100 x 100 that I want to center in an Android app. Is there a way to do this?

I'm thinking it would greatly help with orientation changes for simple apps.

Answer

Konstantin Burov picture Konstantin Burov · Oct 3, 2010

You can use BitmapDrawable for the case. Create centered.xml in res/drawable folder:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/your_image"
    android:gravity="center"/>

Then you can use centered drawable as background.