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
  • Check this medium.com/androiddevelopers/… if it helps Commented Sep 28, 2019 at 14:44
  • with tool_bar layout it's working right? Commented Sep 28, 2019 at 16:19
  • Yes, with tool_bar working nice. Commented Sep 28, 2019 at 16:24

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>
Sign up to request clarification or add additional context in comments.

1 Comment

or the simpler ways is to annotate that <include> layout with tools:viewBindingIgnore="true" like @shweta-chauhan suggested. in my case, this is works
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 Comments

ok. maybe this error is for combining view binding and data binding. View binding doesn't support layout variables
databinding can not be converted to view android, resolved with this attribute thanks :)
thanks, this solution helped me. This is may be due to kotlin extension libray which I am using with databinding
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)
}

Comments

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">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.