Floatingactionbutton with Text Instead of Image

FloatingActionButton with text instead of image

With API 28 you can simply add text to Fabs using:

Visit: https://material.io/develop/android/components/extended-floating-action-button/

 <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:contentDescription="@string/extended_fab_content_desc"
android:text="@string/extended_fab_label"
app:icon="@drawable/ic_plus_24px"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|right|end"/>

Custom Floating Action Button with TextView on top of Image

You can have a transparent FAB, and draw on top of it a layout with your custom Image & TextView;

Similar to FAB, app:layout_anchor & app:layout_anchorGravity can be used on the layout to lay it on top of the FAB.

Here is an implementation:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_cutout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0"
app:layout_anchor="@id/navigation" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
app:layout_anchor="@id/navigation"
app:layout_anchorGravity="center_horizontal">

<com.google.android.material.imageview.ShapeableImageView
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearance="@style/roundedimage"
app:srcCompat="@drawable/with_cart1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="99"
android:textColor="@color/white"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/navigation"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@android:color/holo_blue_dark"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:fabCradleMargin="10dp"
app:fabCradleVerticalOffset="8dp">

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundTint="@android:color/transparent"
app:elevation="0dp"
app:itemIconTint="@android:color/white"
app:itemRippleColor="@android:color/white"
app:itemTextColor="@android:color/white"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu_4" />

</com.google.android.material.bottomappbar.BottomAppBar>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Result:

Sample Image

Floating action button with text label. Flutter

You can use this flutter package flutter_fab
Or you can refer from this article Create Animated FAB in flutter.

How to add a TextView above a FloatingActionButton in Android

You just need to add an elevation attribute to the textview

 <FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#00fff0"
app:borderWidth="0dp"
android:elevation="0dp"
app:fabSize="normal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="above"
android:elevation="7dp"/>

</FrameLayout>

Add text to FAB in Android

You can try with ExtendedFloatingActionButton.

Extended floating action buttons are used for a special type of
promoted action. They are distinguished by an icon and a text floating
above the UI and have special motion behaviors related to morphing,
launching, and the transferring anchor point.

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="8dp"
android:contentDescription="@string/app_name"
android:text="@string/go"
android:textAllCaps="true"
android:padding="8dp"
app:iconPadding="-3dp"
android:gravity="center"
android:drawableTop="@drawable/ic_navigate"
app:layout_anchorGravity="bottom|right|end"/>

result FAB

To use an ExtendedFloatingActionButton we need to import the google material components dependency as shown below

 implementation 'com.google.android.material:material:1.1.0'

In Android studio how to let a image in FloatingActionButton center

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
app:backgroundTint="@color/colortoolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/world"
app:fabSize="mini"/>

You are giving different margin sizes.remove them.



Related Topics



Leave a reply



Submit