How to Reuse Views and Layouts With INCLUDE and MERGE in Android

Complete source code in Android How to Reuse Views and Layouts With INCLUDE and MERGE

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="tech.codingpoint.includemergeexample.MainActivity">

    <include
        android:id="@+id/progressbar1"
        layout="@layout/circular_progressbar" />

    <include
        android:id="@+id/progressbar2"
        layout="@layout/circular_progressbar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="16dp" />

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

</LinearLayout>

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="2.5"
    android:shape="ring"
    android:thickness="4dp"
    android:useLevel="true">

    <solid android:color="@color/colorAccent" />

</shape>

circular_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:progressBarStyleHorizontal"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:progressDrawable="@drawable/circle"
    tools:progress="80" />

two_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Set the IDs here and use them in an activity as usual -->

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

</merge>

Leave a Comment