27

Android Studio 3.6. Canary 12

build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    ext.RETROFIT_VERSION = '2.6.0'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0-alpha12'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

in app/build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"

android {
    viewBinding {
        enabled = true
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    compileSdkVersion 29
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        applicationId "com.android.testproject.android.kotlin"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

in layout 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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="android.view.View" />

        <variable
            name="handler"
            type="com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />

    </data>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            android:title='@{@string/coroutine_retrofit}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/buttonRetry"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/min_height"
            android:visibility="gone"
            android:text="@string/retry"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/agentsRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
            tools:listitem="@layout/agent_list_item" />

        <TextView
            android:id="@+id/noActivityTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/no_agents"
            android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

        <include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

here progress_bar_layout.xml

<?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:id="@+id/containerProgressBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4777"
    android:clickable="true"
    android:elevation="2dp"
    android:focusable="true">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="@dimen/min_height"
        android:layout_height="@dimen/min_height"
        android:indeterminateTint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here CoroutinesRetrofitActivity.kt

class CoroutinesRetrofitActivity : AppCompatActivity(), AgentListItemAdapter.AdapterListener {
    var agentList = ObservableArrayList<Agent>()
    private lateinit var binding: CoroutinesRetrofitActivityBinding
    private lateinit var coroutinesRetrofitViewModel: CoroutinesRetrofitViewModel
    val isVisibleNoItems = ObservableBoolean()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = CoroutinesRetrofitActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.setHandler(this);

 this.coroutinesRetrofitViewModel.getIsShowProgress()
            .observe(this, object : Observer<Boolean> {
                override fun onChanged(isShowProgress: Boolean?) {
                    findViewById<View>(R.id.containerProgressBarLayout).visibility =
                        if (isShowProgress!!) View.VISIBLE else View.GONE
                }
            });

    }

But when I try to build I get error:

> Task :app:checkDebugDuplicateClasses
> Task :app:javaPreCompileDebug

> Task :app:compileDebugJavaWithJavac
\testProjects\android\TestProjectAndroidKotlin\app\build\generated\source\kapt\debug\com\android\testproject\android\kotlin\databinding\CoroutinesRetrofitActivityBindingImpl.java:48: error: incompatible types: ProgressBarLayoutBinding cannot be converted to ViewDataBinding
        setContainedBinding(this.mboundView01);

But if I remove from xml this:

 <include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />

then error is gone.

What is wrong with progress_bar_layout ?

If I direct include progress bar in layout 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>

        <import type="android.view.View" />

        <variable
            name="handler"
            type="com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />

    </data>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            android:title='@{@string/coroutine_retrofit}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/buttonRetry"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/min_height"
            android:text="@string/retry"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/agentsRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
            tools:listitem="@layout/agent_list_item" />

        <TextView
            android:id="@+id/noActivityTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/no_agents"
            android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

        <ProgressBar
            android:id="@+id/containerProgressBarLayout"
            style="?android:attr/progressBarStyle"
            android:layout_width="@dimen/min_height"
            android:layout_height="@dimen/min_height"
            android:indeterminateTint="@color/colorPrimary"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

the problem is gone.

Why with include not work?

include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />
3

4 Answers 4

50

I found solution. If outer xml is data binding xml, then inner xml also must be data binding. So progress_bar_layout.xml must be 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/containerProgressBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#66545a5a"
        android:clickable="true"
        android:elevation="2dp"
        android:focusable="true">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="@dimen/min_height"
            android:layout_height="@dimen/min_height"
            android:indeterminateTint="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>
1
  • 5
    or the simpler ways is to annotate that <include> layout with tools:viewBindingIgnore="true" like @shweta-chauhan suggested. in my case, this is works
    – mochadwi
    Commented Feb 26, 2020 at 10:32
9

In progress_bar_layout use below line (m also not sure it works for you or not because I didn't try yet but if it works then I will add more clarification)

Maybe this error is for combining view binding and data binding. View binding doesn't support layout variables

<androidx.constraintlayout.widget.ConstraintLayout
    ...
    tools:viewBindingIgnore="true" >
...
</androidx.constraintlayout.widget.ConstraintLayout>
3
  • ok. maybe this error is for combining view binding and data binding. View binding doesn't support layout variables Commented Sep 28, 2019 at 16:17
  • 2
    databinding can not be converted to view android, resolved with this attribute thanks :) Commented Jul 1, 2020 at 18:53
  • thanks, this solution helped me. This is may be due to kotlin extension libray which I am using with databinding Commented Feb 17, 2021 at 22:14
4

In addition of the existing answers. If your layout uses layout (data-binding) tag as parent, then you have to wrap your include's layout inside layout (data-binding) tag. Below are the source code.

include's Layout

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <matrixsystems.core.widgets.CustomToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_white"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

</layout>

Layout

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_app" />

Activity

override fun onCreate(savedInstanceState: Bundle?) {
    // getting toolbar from binding
    setSupportActionBar(binding.toolbar.root as Toolbar)
}
3

I used a_subsriber answer and it really works but be sure to put xmlns nampespaces in layout tag but not in view's tag

<layout xmlns:android="http://schemas.android.com/apk/res/android">

Not the answer you're looking for? Browse other questions tagged or ask your own question.