If creating multiple instances of the same layout it is good practice to save that layout code in its own file and instantiate it using the
Given this original layout:
<?xml version="1.0" encoding="utf-8"?> <layout> <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.constraint.ConstraintLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="@string/passenger_label" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView tools:text="@string/passenger_name" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <!-- Start of section of code defining a group of objects in a fixed layout to be duplicated across portrait and landscape --> <ImageView android:id="@+id/leftRectangle" android:layout_width="60dp" android:layout_height="80dp" ... /> <ImageView android:id="@+id/divider" android:background="@color/colorPrimaryLight" ... /> <ImageView android:id="@+id/rightRectangle" android:layout_width="60dp" android:layout_height="80dp" ... /> <TextView android:id="@+id/textViewOriginAirport" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:id="@+id/textViewDestinationAirport" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <ImageView android:id="@+id/imagePlane" android:layout_height="wrap_content" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewFlightCode" tools:text="@string/flight_code" android:layout_width="wrap_content" ... /> <!-- Endof section of code defining a group of objects --> <TextView android:id="@+id/textViewBoardingTimeLabel" android:text="@string/boarding_time_label" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewBoardingTime" tools:text="@string/boarding_time" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewDepartureTimeLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:id="@+id/textViewDepartureTime" tools:text="@string/departure_time" android:layout_width="wrap_content" ... /> </android.support.constraint.ConstraintLayout> </ScrollView> </layout>The extracted code pasted into it separate layout file (name boarding_info.xml) would look like:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" // Very important to include these tags xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <android.support.constraint.ConstraintLayout // Very important to include these tags android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/leftRectangle" android:layout_width="60dp" android:layout_height="80dp" ... /> <ImageView android:id="@+id/divider" android:background="@color/colorPrimaryLight" ... /> <ImageView android:id="@+id/rightRectangle" android:layout_width="60dp" android:layout_height="80dp" ... /> <TextView android:id="@+id/textViewOriginAirport" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:id="@+id/textViewDestinationAirport" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <ImageView android:id="@+id/imagePlane" android:layout_height="wrap_content" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewFlightCode" tools:text="@string/flight_code" android:layout_width="wrap_content" ... /> </android.support.constraint.ConstraintLayout> </layout>And the original file would look like:
<layout> <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.constraint.ConstraintLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="@string/passenger_label" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView tools:text="@string/passenger_name" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <include android:id="@+id/boarding_info" // Essential for binding data layout="@layout/boarding_info" // Only essential attribute - others added on on aligning with surrounding elements android:layout_width="0dp" android:layout_height="wrap_content" ... /> <TextView android:id="@+id/textViewBoardingTimeLabel" android:text="@string/boarding_time_label" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewBoardingTime" tools:text="@string/boarding_time" android:layout_width="wrap_content" ... /> <TextView android:id="@+id/textViewDepartureTimeLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" ... /> <TextView android:id="@+id/textViewDepartureTime" tools:text="@string/departure_time" android:layout_width="wrap_content" ... /> </android.support.constraint.ConstraintLayout> </ScrollView> </layout>If binding to these layouts you will need to add in an extra reference to the include file e.g.
mBinding.textViewOriginAirport.setText("aString");becomes
mBinding.boardingInfo.textViewOriginAirport.setText("aString"); // boardingInfo automatically generated from 'boarding_info' xml name
