Skip to main content

Android FloatingActionButton example.

Floating Action Button represents the primary action in application. They are used for special type of promoted actions. It is shaped like a Circled icon Floating above the UI. Floating Action button has 2 sizes , Default(56dp*56dp) and Mini(40dp*40dp) .  
Floating Action Button is a material design compenent so we need to follow some design guidelines while using it
Important Floating Action Button Desin Guidelines
  • Floating Buttons should only be used for positive actions like create,favorite,share,explore . It should not be used for destructive actions like delete,alerts,errors..
  • It should be placed minimum 16dp from the edge of the screen
  • Icon inside the Floating Action Button should 24dp
  • Only one floating Action button recommended per screen
For more Guidelines visit 

Floting ActionBar has special motion behaviour like morohing,launching and tranferring anchor points. It can Morph into a toolbar showing related actions or it can transform into a single sheet of material containing all actions.

Our Demo application shows the basics of how to use Floating Action Button. Screenshots of the application are



Our demo application contains FloatingActionButton at the end of the screen , a Toast is shown on clicking it.

1. First we need to add  dependency for FloatingActionButton in the app level build.gradle file.
    compile 'com.android.support:design:26.0.0-alpha1'
build.gradle file
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.androidtuts4u.arun.demofab"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:26.0.0-alpha1'
}

2.Next we need to create layout for our application. we are Using Coordinator layout for application. Coordinator layout is super powered frame layout . Main use of using coordiantor layout is that it can coordinate animatons and transition of the views with in it.
Add FloatingActionButton to our layout.
<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_add_white"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"/>
  • app:FabSize  : size Floating action button can be controlled by this xml attribute , we can use two size  Default(56dp*56dp) and Mini(40dp*40dp)
  • app:srcCompat : To choose Icon to be used inside floatingActionButton normally 24 dp icon is used
  • app:backgroundTint : Used to choose colour of our FloatingActionButton , By Default it will be the accent color of ou application theme.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.androidtuts4u.arun.demofab.MainActivity">


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:clickable="true"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_add_white" />
</android.support.design.widget.CoordinatorLayout>

3.Next our MainActivity.java
package com.androidtuts4u.arun.demofab;

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

public class MainActivity extends AppCompatActivity {
    FloatingActionButton fabAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fabAdd = (FloatingActionButton) findViewById(R.id.fab_add);
        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Floating Action Button clicked", Toast.LENGTH_LONG).show();
            }
        });
    }
}

FloatingActionButton is declared inside the MainActivity , OnclickListener is written for the FloatingActionButton and a Toast is shown on clicking.
fabAdd = (FloatingActionButton) findViewById(R.id.fab_add);
fabAdd.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Floating Action Button clicked", Toast.LENGTH_LONG).show();
            }
        });
Source code of this demo application can be downloaded from the following google drive link
https://drive.google.com/file/d/0BySLpWhqmbbdamRJbnRyMG5Ebjg/view?usp=sharing
                        OR Download from  Github
https://github.com/arunkfedex/DemoFab

Comments

Popular posts

Android Sqlite and ListView Example

This is simple application which insert data into Sqlite database --and shows the data  from the database in a ListView ListView is not used in android Anymore . We use RecyclerView and CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with CardView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html This  is the first Activity of application which shows data from database in listview. Register here buton will start a Registration Activity. Submit button will add data to database and show it in the ListView of MainActivity. Update can be performed by clicking ListView items.     you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdS0dtT1R2TXdBWEE&usp=sharing click on the above link ->sign into  your  google acc

Android Card View And RecyclerView Example

CardView Cardview lets you show information inside cards that have consistent look across the platform . CardView is introduced with material design through support v7 library. CardView extends frame layout and It can have shadows and round corners.     Cards can be used as independent views that serves as an entry point to more detailed information. CardView can also  be used with RecyclerView to display cards as list. In this example we are using Cardview with Recyclerview to show data as list of cards. Our Demo application contains vertical list of cards . Each card contain Movie name, movie poster and director name. screenshot of demo app 1. CardView and RecyclerView are supportV7 library widget . so we need to add dependency for both in app level build.gradle file . which is in the app folder compile 'com.android.support:cardview-v7:26.+' compile 'com.android.support:recyclerview-v7:26.+' build.gradle file is apply plugin: 'com.android.applicat

Android RecyclerView Example

RecyclerView RecyclerView is the advanced version of Listview . It has more flexibility than ListView . RecyclerView is Compatible with Api level 7 onward . We need an adpater class and layout manager for creating Recyclerview.  RecyclerView has 3 built in layout managers LinearlayoutManager - this shows items in vertical or horizontal list GridLayoutManger - this shows item in a grid StaggeredGridLayout Manager - this shows item in a staggered grid we can also create custom layout mangers by extending RecyclerView.LayoutManager class RecyclerView does not have a divider to separate ts iitems . if we want divider we need to extend ItemDecoration class to display the divider RecyclerView also does not have an onItemClickListener for onClick events so we need a class extending RecyclerView.OnItemTouchListener for onclick events or we can use onlick listener in our adapter class we use LinearLayoutManager and a custom Adapter class in this example . This Demo app does no