Search code examples
android-appbarlayoutandroidx

androidx appbarlayout problem in migrated project


I recently refactored my project to androidx. but now it has a fatal exception.when i run the project i face the error with coordinatorlayout and appbarLayout and incompatible with androidx. I guess I have to use an androidx alternative for AppBarLayout but I searched for it and can't find any alternative.

android.view.InflateException: Binary XML file line #36: Could not inflate Behavior subclass android.support.design.widget.AppBarLayout$ScrollingViewBehavior
Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass android.support.design.widget.AppBarLayout$ScrollingViewBehavior

this is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:fitsSystemWindows="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:toolbarId="@+id/toolbar"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"
        app:contentScrim="?attr/colorPrimary">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/allAdsMain_recyclerView_mainCategories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".all_ad_main" >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/allAdsMain_recyclerView_ads"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

<Button
    android:id="@+id/allAdsMain_button_adRegister"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:layout_marginBottom="5dp"
    android:background="@drawable/roundshapebtn"
    android:text="@string/call_for_price"
    android:textColor="@color/white"
    android:textDirection="rtl"
    android:textSize="24sp"
    app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • You're still referencing a support library dependency under your NestedScrollView.

    Change:

    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"

    to

    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

    You're already using it in your CoordinatorLayout. Next time you run into this type of issue, you can use code complete in Android Studio while trying to create (in this case) an AppBarLayout and check the package of the version Android Studio wants to import. If your dependencies are setup right, there should only be the androidx option,