Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] How to include an Android layout but also access its elements in binding?

Posted on November 14, 2022

Solution 1 :

Two things are required in order to use ViewBinding with an included layout.

<merge> is not supported

The documentation only covers Data Binding, and not View Binding, but it does appear to be applicable to both. See https://developer.android.com/topic/libraries/data-binding/expressions#includes

Data binding doesn’t support include as a direct child of a merge element.

In other words, the layout must have a real, concrete view as its root element. The following is supported:

<LinearLayout ...>
    <TextView ... />
    <TextView ... />
</LinearLayout>

But a layout with a <merge> root is not supported:

<merge ...>
    <TextView ... />
    <TextView ... />
</merge>

The <include> tag must specify an ID

It is, in general, possible to include a layout without explicitly specifying an ID. View Binding does not support this:

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

Even if the included layout has an ID on its root element, it is still not supported. Instead, you must explicitly specify the ID on the <include> tag:

<include
    android_id="@+id/some_id"
    layout="@layout/included_layout"/>

Once both of these conditions are satisfied, your generated binding for the outer layout will include a reference to the binding of the included layout. Let’s say our two files are outer_layout.xml and included_layout.xml. Then these two files would be generated:

  • OuterLayoutBinding.java
  • IncludedLayoutBinding.java

And you could reference the included views like this:

val outerBinding = OuterLayoutBinding.inflate(layoutInflater)
val innerBinding = binding.someId // uses the id specified on the include tag
val innerView = innerBinding.viewPagerTop

Or, for short:

val binding = OuterLayoutBinding.inflate(layoutInflater)
val innerView = binding.someId.viewPagerTop

Problem :

I’m doind this on my activity:

       <include
        android_id="@+id/withGyroLayout"
        layout="@layout/with_gyro_layout"/>

Where with_gyro_layout.xml is

<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">

    <com.example.util.FixedTransformerViewPager
        android_id="@+id/viewPagerTop"
        android_layout_width="0dp"
        android_layout_height="143dp"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintTop_toTopOf="parent" />

    <com.example.util.FixedTransformerViewPager
        android_id="@+id/viewPagerBottom"
        android_layout_width="0dp"
        android_layout_height="143dp"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintTop_toBottomOf="@+id/viewPagerTop" />
</androidx.constraintlayout.widget.ConstraintLayout>

However, I can’t access the elements viewPagerBottom or viewPagerTop from the binding for my activity:

binding.viewPagerBottom.setVisibility(View.VISIBLE);
binding.viewPagerTop.setVisibility(View.VISIBLE);

I tried putting with_gyro_layout.xml around <merge>...</merge> but it also didn’t solve.

READ  [ANSWERED] android - App is working fine in debug build but not working in production
Powered by Inline Related Posts

I want to be able to change programatically between with_gyro_layout.xml and without_gyro_layout.xml and also access its inner elements by the binding. How can I do that?

Comments

Comment posted by dominicoder

Please post complete layout code, including

Comment posted by Guerlando OCs

I tried to import it but there is none. Even tried restarting Andrid Studio. Also, even if it generates, it wouldn’t be the one inside the activity, I’d have to insert it manually, right? Is it possible?

Comment posted by Guerlando OCs

there exists

Comment posted by Ben P.

@GuerlandoOCs I’ve updated my answer with a lot more detail. I was able to reproduce all of this in my own Android Studio (I’m using version 4.0).

Comment posted by JCarlosR

I remember that binding.someId used to give me a reference of the included layout binding, but now for some reason, it is a LinearLayout (the parent element in the layout instead of the binding reference). Is the short syntax not working anymore?

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress