Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Re-using layouts with include and merge tags in Android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 351
    Comment on it

    In most of the android application we have to reuse certain set of layouts. Every time writing same set of code(xml) slows down the user interface performance. To achieve the feature of re-usabilty in our application we have tags include and merge. include and merge tags embedded the reusable layout in our main layout/current layout.

    Creating reusable layout:

    re_use.xml

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/android" />
    
    </LinearLayout>
    

    Use of include tag: Using include tag we will reuse the component/layout re_use.xml in our main layout.

    main_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <include layout="@layout/re_use"/>    
    
        <TextView
            android:id="@+id/tvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    
    </LinearLayout>
    

    Another way to include layout: In case of overriding the layout attributes. width and height will be necessary in order to take effect.

    <include android:id="@+id/title"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             layout="@layout/re_use"/>
    

    Use of merge tag

    To overcome the redundancy in our code tag is most appropriate to eliminate redundant view groups. It will increase the performance as well as reduces the unnecessary code in our UI.

    re_use.xml

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:id="@+id/tvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello India!"/>
    
    </merge>
    

    main_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <include layout="@layout/re_use"/>
    
        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/android" />
    
    </LinearLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: