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

Simple Calculator in Android

You can view new updated simple calculator with ViemModel and LiveData in my new blog    https://androidtuts4u.blogspot.com/2021/10/simple-calculator-with-viewmodel-and.html To create a calculator first  we need to create the layout of the calculator. Layout  is created  using XML file given below <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >  <EditText     android:id="@+id/result_id"       android:layout_width="fill_parent"     android:layout_height="120dp"   />  <Button    android:id="@+id/Btn7_id"      android:layout_width="70dp"    android:layout_height="60dp"    android:layout_below="@id/result_id"   ...

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 abov...

Keytool is not recognized as internal or extenal command / Adding PATH in system variable

If you are running a keytool command  keytool -list -v -keystore C:\Users\arun\.android\debug.keystore -alias androiddebugkey -storepass android    and getting an error   'keytool' is not recognized as an internal or external command  If you are using any other commad like java,javac , etc.. and getting an error " is not recognized as an internal or external command"  you can also use this same steps  you are getting this error because keytool.exe , executable file which exists in the bin directory of your JDK  is not added to Path in your Environmental variables. To resolve this issue 1 .first we need to find the bin Directory of our jdk    Usually this will be in  C:\Program Files\Java\jre1.8.0_221\bin (jre1.8.0_221 - change this to your latest version , ). you can see keytool.exe file in the bin directory . (If you installed jdk in a different directory Find your Jdk installation folder and  use that path....