October 22, 2024
Chicago 12, Melborne City, USA
Android

FloatingActionButton, layout_anchor and layout_gravity not working API level 34


I am trying to create Bottom menu using BottomAppBar and BottomNavigationView and FloatingActionButton. I have checked below are the solution and also some other github project also.

My Android Studio version is Android Studio Koala Feature Drop | 2024.1.2 Patch 1. Project configuration is compileSdk = 34 and targetSdk = 34

Question1, Question2, Tutorial, Tutorial, Tutorial but non of them worked for me.

Below is my XML :

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/screen_bg">

        
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_60sdp"
            android:layout_gravity="bottom"
            android:background="@color/white"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:elevation="8dp"
            app:fabAlignmentMode="center"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="30dp"
            app:fabCradleVerticalOffset="12dp"
            app:hideOnScroll="true">

            <!-- BottomNavigationView inside the BottomAppBar -->
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:background="@android:color/transparent"
                app:elevation="8dp"
                app:itemIconTint="@android:color/black"
                app:itemTextColor="@android:color/black"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/bottom_nav_menu" />
        </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_ipo"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_marginBottom="@dimen/_30sdp"
            android:backgroundTint="@color/purple_b"
            app:fabSize="normal"
            app:layout_anchor="@id/bottomAppBar"
            app:layout_anchorGravity="center"
            app:maxImageSize="25dp"
            app:shapeAppearance="@style/RoundFAB"
            app:srcCompat="@drawable/menu_ipo"
            tools:ignore="ContentDescription" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Here is my theme.xml:

<style name="Theme.BottomBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
    </style>

This is my Android Studio xml preview

Once I run the project then its displaying as below:

Live preview

and this is my Kotlin file where I can initialize everything:

private lateinit var binding: DashboardWorkingBinding
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.dashboard_working)
        binding.bottomNavigationView.background = null
    }

I don’t know where I am doing wrong,



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video