1

I want to add Floating Icon plus bottom next to the Search Icon for that i try to use

app : layout_anchor="@+id/bottom_nav"

this layout_anchor was showing when i enter the xml code but i try to add xml code after that also the Floating bar not goes after the Search Icon

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        
        app:backgroundTint="@color/colorPrimary"
         app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        /> </androidx.constraintlayout.widget.ConstraintLayout>

also if i remove these lines it Shows me a Error this-view-is-not-constrained-it-only-has-designtime-positions-so-it-will-jump to.....

            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"

bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="@string/home_menu"
    android:id="@+id/home_menu"
    android:icon="@drawable/ic_home"
    />
    <item android:title="@string/search_menu"
        android:id="@+id/search_menu"
        android:icon="@drawable/ic_search"
        />

    <item android:title="@string/list_menu"
        android:id="@+id/list_menu"
        android:icon="@drawable/ic_filter_list"
        />
    <item android:title="@string/settings_menu"
        android:id="@+id/setting_menu"
        android:icon="@drawable/ic_settings"
        />
</menu>
Karthickyuvan
  • 428
  • 2
  • 16

1 Answers1

1

layout_anchor is an attribute of CoordinatorLayout not ConstraitLayout

Try out this

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:src="@drawable/ic_add"
    app:elevation="100dp"
    app:backgroundTint="@color/colorPrimary"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

Or use BottomAppBar with prebuild fab

Vlad
  • 7,997
  • 3
  • 56
  • 43
  • app:layout_constraintEnd_toEndOf="parent" bcoz of this line it add the button to end of the parent but i need Center and next to search icon – Karthickyuvan Aug 03 '20 at 16:25
  • 1
    @Karthickyuvan edited. Change margin bottom to move vertically as you need – Vlad Aug 03 '20 at 16:29
  • the floating bar goes behind the BottomNavigationView but i need to be front – Karthickyuvan Aug 03 '20 at 16:38
  • 1
    @Karthickyuvan try to play with `app:elevation` on both views – Vlad Aug 03 '20 at 16:41
  • [![but when i use elevation both the icons near to the floating button was very close how to add padding for these][1]] [1]: https://i.stack.imgur.com/FeEJV.jpg – Karthickyuvan Aug 03 '20 at 16:56
  • 1
    @Karthickyuvan change `android:layout_marginBottom` – Vlad Aug 03 '20 at 17:01
  • no bro 16 dp is good is it possible to add padding to the menu items ( i think if we move both the search to little left and filter icon to little right ) – Karthickyuvan Aug 03 '20 at 17:07
  • @Karthickyuvan try to add an empty menu item to the center with invisible icon or even without it (just `title` attr) – Vlad Aug 03 '20 at 17:10