Charles
Charles

Reputation: 201

android binding value to include layout is failed,

I try setting value to include layout. I wrapped the root layout to tag and passed using app:headerTitle. But I got this error

AAPT: C:\Users\ckdrb\Desktop\EMOPlayer\app\build\intermediates\incremental\mergeDebugResources\stripped.dir\layout\activity_equalizer.xml:11: error: attribute headerTitle (aka com.jakchang.emo:headerTitle) not found.
error: failed linking file resources.

I don't understand what's wrong, Because I followed so many examples.

equalizer.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/player_background_theme"
    android:orientation="vertical">
    <include
        android:id="@+id/appbar"
        layout="@layout/layout_part_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:headerTitle="bas123"/>
</LinearLayout>
</layout> 

layout_part_appbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="headerTitle"
            type="String" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/loginHeaderLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp">

        <Button
            android:id="@+id/backButton"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="5dp"
            android:background="@drawable/back_arrow_black"
            app:backgroundTint="@null"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/headerTitleText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textAlignment="center"
            android:textSize="@dimen/appbar_title_text_size"
            android:textStyle="bold"
            android:textColor="@color/colorBlack"
            android:layout_marginStart="10dp"
            android:text="@{headerTitle}"
            app:layout_constrainedWidth="true"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/backButton"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Upvotes: 0

Views: 409

Answers (4)

Ashwin Vavaliya
Ashwin Vavaliya

Reputation: 176

Like app:headerTitle="@{@string/app_name}" in equalizer.xml file

Upvotes: 0

NRUSINGHA MOHARANA
NRUSINGHA MOHARANA

Reputation: 1559

I have achieved this via code. First remove app:headerTitle="bas123" declared inside equalizer.xml. Then in fragment or activity set data to variable headerTitle. Look below code:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding = DataBindingUtil.setContentView<EqualizerBinding> 
    (this,R.layout.equalizer)
    binding.appbar.headerTitle = "My Header"
   }

Upvotes: 0

Shlomi Katriel
Shlomi Katriel

Reputation: 2413

In the data section, you need to use fully qualified path or import the class, like in Java.

Using full path:

<data>
    <variable
        name="headerTitle"
        type="java.lang.String" />
</data>

Using import:

<data>
    <import type="java.lang.String"/>

    <variable
        name="headerTitle"
        type="String" />
</data>

Upvotes: 0

Mittal Patel
Mittal Patel

Reputation: 6089

To pass the value using data binding you need to set it as below

app:headerTitle="@{`bas123`}"

Upvotes: 1

Related Questions