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 List View using Custom Adapter and SQLite

following is a simple applicaton to create ListView using  Custom adapter.screenshot of the application  is like this . 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 Example is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html The ListView below the submit button is populated using Custom Adapter.Data is stored and retrieved using SQLite databsase. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdUXE5aTNhazludjQ&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. To create a simple application like this 1.  Create a class which extends  

Android CardView And SQLite example

SQLite Database Android provides several options to save persistent application data. SQlite database is ideal for saving repeating and structured data . Using Sqlite we can save structured data in a private database.  This is a simple application showing use of Sqlite database in android . This example shows how to perform Insert , select , update and delete operation in  SQlite database Screenshots of our sample application                                                                                                                      This is a Registration app. New user can register by clicking registration button . Registered  users are shown in cards below that button . we can update or delete registered users by clicking overflow menu in each card. 1.First we need to create database for our application .    a. Create a contract class .Contract class contains constants that defines names of Tables and columns of our database. Contract class allows us

Google Sign-in for Android App

This is demo application implementing Google Sign-in in android application. Screenshots of the application are when you click on the sign in button popup will be shown to select google account for sign in . After signing in , user can see his profile photo,name and Email id in the login screen . User can sign out using signout button . Disconnect button will provide user ability to disconnect their google account from the app. Before we start coding for our application , we need to turn on sign-in API for our app in google developer console. Go to this link to know  how to turn on google sign-in api for our app https://androidtuts4u.blogspot.com/2019/09/enabling-sign-in-api-in-google.html now we successfully configured Google Api console project .  we can start coding our application. I . first we need to configure build.gradle file we need to add two dependencies   1. google play service dependency for google sign-in.    2 . we are using third party library