Search code examples
androidandroid-coordinatorlayoutbottomnavigationview

App crashes when try to get `BottomNavigationView`'s layout params


I want hide my bottomNavigationView when scrolling

this is my activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    tools:context=".CircularActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottomBar">
            <include layout="@layout/content_main" />
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true">
            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottomNavView_Bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:background="?android:attr/windowBackground"
                android:foreground="?attr/selectableItemBackground"
                app:itemBackground="@color/bgBottomNavigation"
                app:itemIconTint="@android:color/white"
                app:itemTextColor="@android:color/white"
                app:menu="@menu/navigation">
            </android.support.design.widget.BottomNavigationView>
        </RelativeLayout>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

I wrote my BottomNavigationViewHelper but when I set behavior:

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewHelper());

I get this error:

android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams

UPDATE: when i put BottomNavigationView directly inside the CoordinatorLayout my view change and is not good: enter image description here

UPDATE2: I fixed by changing some attrb:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    tools:context=".CircularActivity">
    <include layout="@layout/content_main"/>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavView_Bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        android:foreground="?attr/selectableItemBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

Solution

  • The problem is that you try to cast RelativeLayout.LayoutParams to CoordinatorLayout.LayoutParams

    The parent of your BottomNavigationView should be CoordinatorLayout. So try to put BottomNavigationView directly inside the CoordinatorLayout