Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint layout with data binding

Tags:

android

When use simple constraint layout it's working fine but when use data binding then it's not running,and preview is randering wrong and also binding methods are not finding show error **Found data binding errors. ****/ data binding error ****msg:Listener class android.view.View.OnClickListener with method onClick did not match signature of any method item::onCardClick /app/src/main/res/layout/activity_main.xml loc:16:27 - 16:43 ****\ data binding error ******

Without data binding

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/linearLayout"
    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="wrap_content"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:paddingTop="14dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Hello First"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toStartOf="@id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/imageView"/>

    <TextView
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Hello Second"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toStartOf="@id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/line"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1"
        android:adjustViewBounds="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="14dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"/>
</android.support.constraint.ConstraintLayout>

enter image description here With databinding

<?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="item"
            type="com.rv.cnstnt_example.MyPojoClass"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:onClick="@{item::onCardClick}"
        android:paddingTop="14dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="@{item.name}"
            app:layout_constraintBottom_toTopOf="@id/button"
            app:layout_constraintEnd_toStartOf="@id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/imageView"/>

        <TextView
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="@{item.dob}"
            app:layout_constraintBottom_toBottomOf="@id/imageView"
            app:layout_constraintEnd_toStartOf="@id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:layout_constraintBottom_toTopOf="@id/line"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round"/>

        <TextView
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="14dp"
            android:background="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/imageView"/>
    </android.support.constraint.ConstraintLayout>
</layout>

enter image description here MyPojoClass.java

   public class MyPojoClass
{
    private String name = "Vineet";
    private String dob = "22 jun 2018";

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDob()
    {
        return dob;
    }

    public void setDob(String dob)
    {
        this.dob = dob;
    }

    public void onCardClick(){
        Log.e("DATA","CLICK");
    }
}
like image 684
vineet sharma Avatar asked Feb 04 '26 17:02

vineet sharma


1 Answers

in MyPojoClass class change the method onCardClick to accept View as an argument. It should be

public void onCardClick(View view) { 
     Log.e("DATA","CLICK"); 
}
like image 131
Atif Farrukh Avatar answered Feb 07 '26 17:02

Atif Farrukh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!