0

We are re-designing our Android app and I have created the basic layout in a XML file. I am planning to re-use the same layout in multiple activities but I'm not sure how to include this in a new activity and change just the inner contents (inside the NestedScrollView)? I'll be using this layout in different journeys so can't change those to one-activity and multiple fragments? Is there a clever way of including the base outer layout and change the inner contents in XML? Thanks

Base layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey_10">

<com.google.android.material.appbar.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/grey_10">

    <include
        android:id="@+id/options_toolbar"
        layout="@layout/toolbar_simple"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container_modes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_10"
        android:paddingTop="@dimen/journey_planner_top_padding">

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:paddingTop="12dp"
            android:paddingBottom="24dp"
            android:paddingStart="30dp"
            android:paddingEnd="30dp"
            android:textSize="16sp"
            app:fontFamily="@font/johnston100_light"
            android:textColor="@color/johnstonBlack"
            android:text="Text"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
AndroidDev
  • 5,193
  • 5
  • 37
  • 68
  • 1
    I'll be using this layout in different journeys so can't change those to one-activity and multiple fragments? Was that a question or you are saying you can't use fragments? It's confusing – javdromero Jul 20 '22 at 15:58
  • Only this constraint layout android:id="@+id/container_modes"? – Gobu CSG Jul 20 '22 at 15:58
  • @javdromero I know its easy if we have just one activity with this outer layout and then have different fragments for the inner layouts. But in my case because of the completely different journeys I will have to use multiple activities and hence checking if there is a way to re-use. Thanks – AndroidDev Jul 20 '22 at 16:07
  • @GobuCSG Yes, I want to use the same layout in all the activities but the inner layout (inside the NestedScrollView) would be different. So the constraint layout changes in each activity. – AndroidDev Jul 20 '22 at 16:09
  • You're going to using this coordinator layout in your different activities. Nested scroll child should change if I'm right.. Add it run time nestedScrollView.addView(yourlayoutview); If it's main container it's not recommanded. An activity should have a layout it's a clear business... don't make tightly coupled. – Gobu CSG Jul 20 '22 at 16:46

0 Answers0