-1

I defined a layout in this way:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <eo.view.batterymeter.BatteryMeterView
        android:id="@+id/batteryMeter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        app:layout_constraintBottom_toTopOf="@id/colorInputView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

    <eo.view.colorinput.ColorInputView
        android:id="@+id/colorInputView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/batteryMeter" />

</androidx.constraintlayout.widget.ConstraintLayout>

this's the layout file included:

<?xml version="1.0" encoding="utf-8"?>
<merge 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"
    tools:showIn="@layout/activity_color_edit">

    <eo.view.batterymeter.BatteryMeterView
        android:id="@+id/chargingBatteryMeter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintBottom_toBottomOf="@id/batteryMeter"
        app:layout_constraintEnd_toStartOf="@id/criticalBatteryMeter"
        app:layout_constraintStart_toStartOf="@id/batteryMeter"
        app:layout_constraintTop_toTopOf="@id/batteryMeter" />

    <eo.view.batterymeter.BatteryMeterView
        android:id="@+id/criticalBatteryMeter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintBottom_toBottomOf="@id/chargingBatteryMeter"
        app:layout_constraintEnd_toStartOf="@id/unknownBatteryMeter"
        app:layout_constraintStart_toEndOf="@id/chargingBatteryMeter"
        app:layout_constraintTop_toTopOf="@id/chargingBatteryMeter" />

    <eo.view.batterymeter.BatteryMeterView
        android:id="@+id/unknownBatteryMeter"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintBottom_toBottomOf="@id/chargingBatteryMeter"
        app:layout_constraintEnd_toEndOf="@id/batteryMeter"
        app:layout_constraintStart_toEndOf="@id/criticalBatteryMeter"
        app:layout_constraintTop_toTopOf="@id/chargingBatteryMeter" />

    <androidx.constraintlayout.widget.Group
        android:id="@+id/indicatorBatteryMeterGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:constraint_referenced_ids="chargingBatteryMeter,criticalBatteryMeter,unknownBatteryMeter" />

</merge>

In the source code i deflate the layout:

private lateinit var binding: ActivityColorEditBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityColorEditBinding.inflate(layoutInflater)
    val view = binding.root

    setContentView(view)

    setupActionBar()
    setupBatteryMeters()
    setupColorInput()
}

For some reason items inside included file are not bound and thus references to such items are not working, for example in this method binding.indicatorBatteryMeterGroup is not recognized:

private fun setupBatteryMeters() {
    when (colorType) {
        ColorType.INDICATOR -> {
            setupIndicatorBatteryMeters()
            binding.batteryMeter.isInvisible = true
            binding.indicatorBatteryMeterGroup.isVisible = true
        }
        else -> {
            setupBattery(binding.batteryMeter)
            binding.indicatorBatteryMeterGroup.isVisible = false
            binding.batteryMeter.isVisible = true
        }
    }
} 
weirdgyn
  • 886
  • 16
  • 47

1 Answers1

1

Give your include element an ID:

<include layout="@layout/include_battery_meters_with_indicator"
    android:id="@+id/battery_meters" />

Then you can access them as a property of a property of your binding, for instance, binding.batteryMeters.criticalBatteryMeter.

Don't put an ID on the merge element. IIRC, this causes it not to work properly. The ID has to be on the include.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • It seems to work but on build I got this: `e: ColorEditActivity.kt: (123, 38): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type IncludeBatteryMetersWithIndicatorBinding?` – weirdgyn Aug 04 '21 at 19:41
  • Sounds like you have defined that property as nullable when it should be `lateinit` non-nullable. – Tenfour04 Aug 04 '21 at 19:43
  • As you can see in the code above is defined as `lateinit` as you suggest – weirdgyn Aug 04 '21 at 19:44
  • Oh, I see the problem. I would consider it a bug of view binding, but there is a work-around [here](https://stackoverflow.com/questions/58730127/viewbinding-how-to-get-binding-for-included-layouts). I'll delete this answer later. My solution works when the child is not a `merge` element. See part 2 of the top answer on that other question for how to handle it when the child is a `merge` element. – Tenfour04 Aug 04 '21 at 19:52