Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a background image in Android

Tags:

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.

like image 350
NotACleverMan Avatar asked Oct 02 '10 23:10

NotACleverMan


2 Answers

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.

like image 150
Konstantin Burov Avatar answered Oct 05 '22 23:10

Konstantin Burov


Or if you want to use your mipmap image, you should use item instead of bitmap:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@color/app_bg_color" />     <item android:drawable="@mipmap/ic_launcher_round"     android:gravity="center"/> </layer-list> 
like image 28
Kirill Karmazin Avatar answered Oct 06 '22 00:10

Kirill Karmazin