I am trying to use an image as background in my android app. If the image doesn't fit the screen I want the image to be centered horizontally and topped vertically. The remaining screen area should be filled by repeating the edges.
My layout xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/background_image"
>
<ScrollView
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
...
</ScrollView>
</LinearLayout>
I tried to mark the left and right column as well as the top row of pixels as stretchable using draw9patch. That seems to work on smaller devices, but not on Galaxy Tab 10.1 and Motorola Xoom. The edge is repeated vertically but not horizontally.
I also tried to create an xml-drawable with tileMode="clamp"
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:filter="true"
android:gravity="center"
android:tileMode="clamp"
android:src="@drawable/background"
/>
but that doesn't allow me to center the image horizontally. How can I combine clamp and center_horizontal?
Per the documentation "Gravity is ignored when the tile mode is enabled."
A custom drawable can be used to do this. Here is an example of clamping on the left and right with a horizontally centered image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With