Sunday, May 24, 2015

How to scale RelativeLayout background image in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#ffff4966"
    >
    <!--
        We cannot manage RelativeLayout background image scaling.
        So, to manage RelativeLayout background image, you
        can put the image inside an ImageView widget and put it
        center in layout. Then manage the image scaling by ImageView
        widget's attributes.
    -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/animal"
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
        />
</RelativeLayout>
More android examples