Search code examples
androidandroid-toast

Android : Floating Action Bar on the Top of Bottom Navigation is Not Working


In my app i placed a Bottom navigation Bar and also i add the Floating Button with elevation of the bottom navigation Bar

And i place a Toast Msg To Check Whether the Floating Button was Worked or Not But It Not Working

bottom_menu_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<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: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"
        android:tint="@color/colorWhite"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchor="@id/bottom_nav"
        app:elevation="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

In My activity_main.xml i included a bottom_menu_bar.xml File and i Include my Secondactivity.xml file in activity_main.xml

<RelativeLayout

    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"
    android:layout_alignParentTop="false"
    tools:context=".MainActivity">

<include
    android:id="@+id/empty_list"
    layout="@layout/empty_list" />

    <include
        android:id="@+id/bottom_nav"
        layout="@layout/bottom_menu_bar"
      />

MainActivity.Java

public class MainActivity extends AppCompatActivity {

  

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "FloatingPlus Button was Clicked ", Toast.LENGTH_SHORT).show();
              
            }
        });
    }
}

Solution

  • Add these Two Lines inside your fab button

    android:clickable="true"
        android:focusable="true"
    

    Remove the empty_list in mainactivity.xml