Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
Inflate nested layout in Koltin

I have a Constraint layout which 2 main elements inside it: a nested scroll view with a list of elements defined inside another layout file and a textview used as a footer. This is the xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/menu_scrollview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".sections.main.menu.MenuFragment">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/menu_title"
                style="@style/AppTheme.TextTitle1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/mid_small_margin"
                android:text="@string/menu_title"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


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

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
    
    <TextView
        android:id="@+id/menu_title_2"
        style="@style/AppTheme.TextTitle1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/mid_small_margin"
        android:text="@string/menu_title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

This is the list_item_menu_fragment.xml layout defined in it:

<?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"
    android:id="@+id/menu_elements_list"
    style="@style/RectangularTextViewMenu"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="@dimen/mid_small_margin"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="32dp"
    android:orientation="vertical"
    app:layout_constraintTop_toBottomOf="@+id/menu_item_profile"
    app:layout_constraintBottom_toBottomOf="@+id/links_title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/charge_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fontFamily="@font/montserrat_bold"
        android:gravity="center_vertical"
        android:textColor="@color/black"
        android:text="@string/menu_pay_charge"
        app:drawableStartCompat="@drawable/ic_menu_charge"
        app:drawableEndCompat="@drawable/ic_arrow_right" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/pay_online_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fontFamily="@font/montserrat_bold"
        android:gravity="center_vertical"
        android:textColor="@color/black"
        android:text="@string/menu_pay_online"
        app:drawableStartCompat="@drawable/ic_menu_pay_online"
        app:drawableEndCompat="@drawable/ic_arrow_right" />
</LinearLayout>

This is my fragment:

class MenuFragment : Fragment() {

private var _binding: FragmentMenuBinding? = null
private val menuViewModel by viewModels<MenuViewModel>()

private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentMenuBinding.inflate(inflater, container, false)
    return binding.root
}

If I tryo access to my list of elements using binding.chargeButton it says that i cannot do it (because it is in another layout). If I do binding.menuTitle it works perfectly. How can I access to those elements?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As written in ViewBinding

An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

You have to assign an ID to the included layout and then you can call it from the parent view binding class.

Change from

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

to

<include android:id="@+id/listItemMenu" layout="@layout/list_item_menu_fragment" />

Now you can call binding.listItemMenu.chargeButton

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!