Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FloatingActionButton with BottomNavigationView

I cant position my FAB. It should be bottom right, but on top of the BottonNavigationView.

1) Can i achieve this without RelativeLayout inside coordinateLayout?

2) Show me how

3) Should i use FrameLayout as a container for fragments?

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

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

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_electricity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchorGravity="bottom|right|end"
    app:srcCompat="@drawable/ic_add_electro" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom">

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

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

result

like image 220
Xmstr Avatar asked Mar 30 '17 21:03

Xmstr


People also ask

How do I add a floating button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

What is a floating action button?

Floating action buttons (or FAB) are: “A special case of promoted actions. They are distinguished by a circled icon floating above the UI and have special motion behaviors, related to morphing, launching, and its transferring anchor point.”

How do I add a bottom navigation bar in flutter?

In Flutter application, we usually set the bottom navigation bar in conjunction with the scaffold widget. Scaffold widget provides a Scaffold. bottomNavigationBar argument to set the bottom navigation bar. It is to note that only adding BottomNavigationBar will not display the navigation items.


2 Answers

A quick search to FloatingActionButton.Behavior tells us the following thing.

@Override
public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp) {
    if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
        // If the developer hasn't set dodgeInsetEdges, lets set it to BOTTOM so that
        // we dodge any Snackbars
        lp.dodgeInsetEdges = Gravity.BOTTOM;
    }
}

Since BottomNavigationView also resides at the bottom of the layout. Add following element to your BottomNavigationView and CoordinatorLayout will handle the inset and dodging for you automatically.

app:layout_insetEdge="bottom"
like image 194
LightYearsBehind Avatar answered Sep 17 '22 09:09

LightYearsBehind


  1. If you stick it in a linear layout and move the bottom nav outside of the coordinator view, it will be below it on the screen.

  2. You could do it this way, but I prefer using an empty fragment container which I put in the hosting activity, and then place the fragment inside of it in code.

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
           app:popupTheme="@style/AppTheme.PopupOverlay">
    
        </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.AppBarLayout>
    
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    
    </FrameLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_electricity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        app:srcCompat="@drawable/ic_add_electro"/>
    
    
    
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom">
    
    </android.support.design.widget.BottomNavigationView>
    </LinearLayout>
    
like image 41
parkgrrr Avatar answered Sep 17 '22 09:09

parkgrrr