Advanced Android - Floating Action Menu



Floating action menu is new concept which is used in mobile apps and websites. Nowadays some popular sites and apps have used floating action menu including inbox

Example

This example demostrate about how to integrate Android Floating Action Menu.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:fab = "http://schemas.android.com/apk/res-auto"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical">
   <TextView
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:padding = "@dimen/activity_horizontal_margin"
      android:text = "tutorialspoint" />
   <com.github.clans.fab.FloatingActionMenu
      android:id = "@+id/fab_menu"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:layout_alignParentBottom = "true"
      android:layout_alignParentRight = "true"
      android:paddingBottom = "@dimen/activity_horizontal_margin"
      android:paddingRight = "@dimen/activity_horizontal_margin"
      fab:menu_backgroundColor = "#ccffffff"
      fab:menu_fab_label = "Choose an action"
      fab:fab_colorNormal = "#DA4336"
      fab:fab_colorPressed = "#E75043"
      fab:fab_colorRipple = "#99FFFFFF"
      fab:fab_showShadow = "true"
      fab:menu_labels_colorNormal = "#333333"
      fab:menu_labels_colorPressed = "#444444"
      fab:menu_labels_colorRipple = "#66FFFFFF"
      fab:menu_labels_showShadow = "true"
      fab:menu_labels_maxLines = "-1"
      fab:menu_labels_position = "left"
      fab:menu_openDirection = "up"
      fab:fab_shadowColor = "#66000000"
      fab:menu_labels_ellipsize = "end"
      fab:menu_labels_singleLine = "true">
      <com.github.clans.fab.FloatingActionButton
         android:id = "@+id/fab1"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@android:drawable/ic_menu_edit"
         fab:fab_label = "Edit an item"
         fab:fab_size = "mini" />
      <com.github.clans.fab.FloatingActionButton
         android:id = "@+id/fab2"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@android:drawable/ic_menu_add"
         fab:fab_label = "Menu item 2"
         fab:fab_size = "mini" />
      <com.github.clans.fab.FloatingActionButton
         android:id = "@+id/fab3"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@android:drawable/ic_menu_delete"
         fab:fab_label = "tutorialspoint"
         fab:fab_size = "mini" />
   </com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;

public class MainActivity extends AppCompatActivity {
   private FloatingActionMenu fam;
   private FloatingActionButton fabEdit, fabDelete, fabAdd;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      fabAdd = (FloatingActionButton) findViewById(R.id.fab2);
      fabDelete = (FloatingActionButton) findViewById(R.id.fab3);
      fabEdit = (FloatingActionButton) findViewById(R.id.fab1);
      fam = (FloatingActionMenu) findViewById(R.id.fab_menu);
      
      fam.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
         @Override
         public void onMenuToggle(boolean opened) {
            if (opened) {
               showToast("Menu is opened");
            } else {
               showToast("Menu is closed");
            } 
         } 
      }); 
      
      //handling each floating action button clicked
      fabDelete.setOnClickListener(onButtonClick());
      fabEdit.setOnClickListener(onButtonClick());
      fabAdd.setOnClickListener(onButtonClick());
      
      fam.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            if (fam.isOpened()) {
               fam.close(true);
            } 
         }
      }); 
   }
   
   private View.OnClickListener onButtonClick() {
      return new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            if (view == fabAdd) {
               showToast("Button Add clicked");
            } else if (view == fabDelete) {
               ("Button Delete clicked");
            } else {
               showToast("Button Edit clicked");
            } 
            fam.close(true);
         }
      }; 
   } 
   
   private void showToast(String msg) {
      Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
   }
}

Step 4 − Add the following code to build.gradle

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.1'
   compile 'com.github.clans:fab:1.6.4'
}

Step 5 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Floating Menu
Advertisements