27

I new in android and I've troubles with FloatingActionButton behaivors

My custom behavoir class:

public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
    private static final String TAG = "ScrollingFABBehavior";

    public ScrollingFABBehavior(Context context, AttributeSet attrs,
            Handler mHandler) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View directTargetChild, View target,
            int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child,
                        directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton child, View target, int dxConsumed,
            int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
                dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() == View.GONE) {
            child.show();
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout,
            FloatingActionButton
            child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }
}

Fragment XML:

...

<android.support.design.widget.FloatingActionButton
        android:id="@+id/share_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/action_share"
        android:elevation="@dimen/fab_elevation"
        android:src="@drawable/ic_share"
        app:layout_behavior=".ScrollingFABBehavior"/>

</android.support.design.widget.CoordinatorLayout>

RuntimeError when fragment inflate xml:

07-14 08:52:43.904 30785-30785/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.xyzreader, PID: 30785
                                                                       android.view.InflateException: Binary XML file line #115: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                       Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.example.xyzreader.ui.ScrollingFABBehavior
                                                                           at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:615)
                                                                           at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2652)

e.t.c

Whats wrong?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Aleksey Leonov
  • 341
  • 1
  • 3
  • 9

8 Answers8

40

Add the following two constructors to your FooterBehavior:

public FooterBehavior() {
}

public FooterBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}
Onik
  • 15,592
  • 10
  • 57
  • 79
PangoSea
  • 539
  • 3
  • 10
  • doesn't help in my case – Leo Droidcoder Jun 07 '19 at 11:19
  • 3
    Kotlin Version: ```class ScrollingFABBehavior(context: Context?, attrs: AttributeSet?) : FloatingActionButton.Behavior(context, attrs) { constructor() : this(null, null) } ``` – Alqueraf Mar 10 '20 at 11:22
  • There is a simplified Kotlin version possible: `class ScrollingFABBehavior @JvmOverloads constructor(context: Context? = null, attrs: AttributeSet? = null) {...}` – S-Sh Mar 12 '21 at 17:48
17

Make sure that you are using the correct path of the custom behavior class.

For example:

app:layout_behavior="net.company.myapp.view.behavior.TextViewBehavior"
Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Mahmoud
  • 1,672
  • 1
  • 18
  • 23
  • 3
    In my case Android Studio changed my absolute path to a relative path. My package name differs from the id so it was breaking it. – P Fuster Oct 15 '19 at 13:39
  • 6
    Try to use the **absolute path** for the Behavior Subclass instead of **Relative Path** as at runtime Android might not be able to resolute the relative path – Abhishek Nov 28 '19 at 11:05
13

If you are using AndroidX (open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack) then you need to update XML.

Find your element here and replace:

Support:

android.support.design.widget.FloatingActionButton

AndroidX:

com.google.android.material.floatingactionbutton.FloatingActionButton
Paul Spiesberger
  • 4,334
  • 1
  • 37
  • 47
Md Imran Choudhury
  • 6,954
  • 2
  • 45
  • 50
9

In case your app has a different package ID than your actual package structure, make sure you specify the full path to your custom behavior class:

<android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.example.xyzreader.ScrollingFABBehavior"/>
marius bardan
  • 4,397
  • 2
  • 26
  • 31
8

If you are using BottomSheet in Android X you must use

app:layout_behavior="@string/bottom_sheet_behavior"

if not then use

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
trickymind
  • 257
  • 2
  • 12
Ram Chhabra
  • 235
  • 3
  • 8
4

Solved. Change app:layout_behavior=".ScrollingFABBehavior"/> to app:layout_behavior=".ui.ScrollingFABBehavior"/>

GuilhE
  • 10,723
  • 13
  • 63
  • 94
Aleksey Leonov
  • 341
  • 1
  • 3
  • 9
4

I had a similar problem and ended up on this page.

I am working with Bottom Sheet on Android X

I ran my app and got the following error message when I tapped to move to a new Fragment in the app and the app crashed with a RuntimeExeception: "Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehaviour"

My layout attributes were as follows:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/player_bg"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehaviour"
    app:behavior_hideable="true"
    app:behavior_peekHeight="70dp">

When I got that error message I changed one line and then the app worked.

As described above, as I am using Android X, i changed from:

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

I changed this line to:

app:layout_behavior="@string/bottom_sheet_behavior"

This one change meant that the code worked for me, no crash and the new Fragment showed up as expected.

3

If you are working with androidX then your root layout should be:

<androidx.coordinatorlayout.widget.CoordinatorLayout

and bottom_sheet's layout should include:

app:layout_behavior="@string/bottom_sheet_behavior"

else if you are not using androidx or using support library then your root layout should be:

<android.support.design.widget.CoordinatorLayout

and bottom_sheet's layout should include:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"