Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FAB transform to menu

As per the updated material guidelines, the floating action button can transform into a menu as shown below:

Fab to menu

Can someone help me with some code or a some links on how to implement this behavior?

Thank you.

like image 280
Mervin Hemaraju Avatar asked May 16 '20 13:05

Mervin Hemaraju


People also ask

How do I change the shape of the floating action button on Android?

To change the shape of the Floating action button: You can use the shape property of FloatingActionButton() widget class. Implement BeveledRectangleBorder( ) on this property. The output will be a square floating action button, increase the border-radius value to make a circular type shape.

How do I add a floating action 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.”


1 Answers

Try this way

add below dependencies

implementation 'com.google.android.material:material:1.2.0-alpha05'

Layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.circularreveal.CircularRevealFrameLayout
        android:id="@+id/sheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:background="@android:color/transparent"
        android:visibility="invisible"
        app:layout_behavior="@string/fab_transformation_sheet_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="#2196F3"
                android:padding="10dp"
                android:text="ASK Nilesh" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="#FF9800"
                android:padding="10dp"
                android:text="ASK Nilesh" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="#9C27B0"
                android:padding="10dp"
                android:text="ASK Nilesh" />

            <TextView
                android:id="@+id/tvClose"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="#9C27B0"
                android:padding="10dp"
                android:text="Close" />


        </LinearLayout>
    </com.google.android.material.circularreveal.CircularRevealFrameLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Activity Code

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fabMenu.setOnClickListener {
            fabMenu.setExpanded(true)
        }
        tvClose.setOnClickListener {
            fabMenu.setExpanded(false)
        }
    }
}

You can find complete example here https://github.com/askNilesh/floating_action_button_menu OUTPUT

OUTPUT

like image 122
AskNilesh Avatar answered Oct 23 '22 10:10

AskNilesh