BottomSheetBehavior not in androidX libraries
Asked Answered
L

5

104

I was using the BottomSheetBehavior with the original support library:

implementation 'com.android.support:design:27.1.1' 

When I migrated to use the new androidx libraries though the BottomSheetBehavior is missing. The mapping from the above support library isn't in the AndroidX Refactoring List either, but the migration tool removed it.

What am I missing to include the BottomSheetBehavior with the new androidx libraries.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // ReactiveX
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'

    implementation 'com.android.support:design:28.1.0'

    // Android Compatability Libraries
    // Version: https://developer.android.com/topic/libraries/support-library/refactor
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-beta01'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'

    // Android Navigation Component
    // Check here for updated version info - will move to androidx soon.
    // https://developer.android.com/topic/libraries/architecture/adding-components
    def nav_version = "1.0.0-alpha04"

    // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
    androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"

    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
Lizbeth answered 31/7, 2018 at 15:37 Comment(0)
L
237

It turns out that the refactor tool in Android Studio Refactor > Migrate to AndroidX didn't correctly migrate the XML for the BottomSheetBehaviour.

The old location was android.support.design.widget.BottomSheetBehavior, and was not modified by the migration tool. The original XML was:

<fragment
    android:id="@+id/player_bottom_sheet_fragment"
    android:name="app.rxsongbrowsertrials.ui.player.PlayerToggleFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_hideable="false"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    />

The new location is com.google.android.material.bottomsheet.BottomSheetBehavior, so the layout needs to become:

<fragment
    android:id="@+id/player_bottom_sheet_fragment"
    android:name="app.rxsongbrowsertrials.ui.player.PlayerToggleFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_hideable="false"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    />
Lizbeth answered 31/7, 2018 at 19:6 Comment(2)
I spent all day on this one. Hopefully this bubbled up for people to discover more easily.Wroth
In the last update from AS still not corrected this error in the AndroidX migration. ThanksAphasia
S
59

You could also replace

    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
or 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

by

app:layout_behavior="@string/bottom_sheet_behavior"
Silverware answered 21/10, 2018 at 15:19 Comment(1)
My project, generated from an Android Studio template, didn't have @string/bottom_sheet_behavior. I think I was able to pull it in by adding implementation "com.google.android.material:material:1.1.0-alpha04" to my app/build.gradleMacgregor
S
27

You have to import the Material Components Library provided by Google.

Material Components for Android is a drop-in replacement for Android's Design Support Library.

Add in your build.gradle:

implementation 'com.google.android.material:material:x.x.x'

Then use the class com.google.android.material.bottomsheet.BottomSheetBehavior.

In your layout you can use the attribute:

    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    ..>

or

app:layout_behavior="@string/bottom_sheet_behavior"
Subequatorial answered 23/9, 2019 at 23:33 Comment(0)
G
2

For R class

com.google.android.material.R.id.design_bottom_sheet

instead of

android.support.design.R.id.design_bottom_sheet
Gabrielgabriela answered 9/4, 2022 at 11:33 Comment(0)
C
1

I got this error message:

Didn't find class "com.google.android.material.bottomsheet.BottomSheetBehaviour"

The only way to solve this was to change the XML:

Change:

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

Change to:

app:layout_behavior="@string/bottom_sheet_behavior"

This solved the problem

Cytochemistry answered 25/1, 2021 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.