Home » Android » Android – xml layout as a background

Android – xml layout as a background

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is it possible to make a xml background composed of multiple image / scale / alignement?
And so to have a complex drawable xml usable as a background for android layout.

For example, this is the dynamic xml background that I would like to do:
enter image description here

And so, for all of my different activities, put my different layouts/views above:
enter image description here

I know I cannot set a layout as a layout_background, but here is the design of what I would like to do (according to previous pictures):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_image" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_image" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/footer_image" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

I saw we can set bitmap to drawable xml, but for scale and alignement?
Is there an easier way to make this visual rendering?

How to&Answers:

You can create one Layout as you want in dynamic xml background and can Include in all screens behind RelativeLayout like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/my_dynamic_layout"/>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

    ...Your layout code ...

    </RelativeLayout >

</RelativeLayout >

Answer:

I am quite sure what is your requirement, Assuming that you want to put some xml files with different images inside a single xml file I am giving my answer.

Create 3 layouts according to your requirement and then create a main layout where you want to put these 3 layouts.

  • Ex: suppose you have

one.xml

two.xml

three.xml

and lets assume that you have applied background of your choice according to your requirements to all the above layout

now in main.xml

< LinearLayout
---
--->
< include
android:layout_width="fill_parent" android:layout_height="wrap_content"
layout="@layout/one.xml" />
< include
android:layout_width="fill_parent" android:layout_height="wrap_content"
layout="@layout/two.xml" />
< include
android:layout_width="fill_parent" android:layout_height="wrap_content"
layout="@layout/3.xml" />
< /LinearLayout>

arrange the layout according to your requirement and if you need to divide these layouts with some percentage use weight=”.50″

Hope that it works for you.