DataBinding is not working in Android Studio

Viewed 793

I try to get data binding to work, not really successful. Android Studio Version: 2020.3.1 (Ubuntu) com.android.tools.build:gradle:7.0.0

My build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "org.jetbrains.kotlin.kapt"}apply plugin: 'kotlin-kapt'android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.app"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    dataBinding {
        enabled = true
    }

    kapt {
        generateStubs = true
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    dependenciesInfo {
        includeInApk true
        includeInBundle true
    }
    compileSdkVersion 30
    buildToolsVersion '31.0.0'
    ndkVersion '23.0.7599858'}dependencies {

    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    def appcompat_version = "1.3.1"

    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"

    def plugin_version = "3.5.0"
    kapt "com.android.databinding:compiler:$plugin_version"

}

My layoutFile is like this:

<?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"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable name="modul" type="app.view.app.main.ModuleView"/>

    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.view.app.MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{modul.name}"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="173dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

And the Class looks like this:

package app.view.app.main

class ModuleView {
    var name:String ="TestModuleView"

}

What I want to do, the variable in the class (name) appears in the TextView.

Can anybody help me ? I am new on Android Studio.

Thanx Senifor

1 Answers

It might be possible that you have not initialized the ModuleView class in the Activity/Fragment. You need to initialize the module the following way. e.g.

val module = ModuleView()
module.name = "Sample Name"
binding.modul = module
Related