Introduction
Floating Action buttons are a bit different from the regular buttons. They are UI implemented for primary or promoted actions. There are four basic types of Floating Actions buttons, namely.
- Normal Floating Action buttons
- Mini Floating Action buttons
- Extended Floating Action buttons
- Theming Floating Action buttons
This article will discuss the two most commonly used Floating Action buttons, Normal Floating Action buttons and Mini Floating Action buttons.
Normal FAB
The Normal FAB(Floating Action buttons) are not contracted or expanded and are of regular size. The Normal FAB is of 56 dp radius.
First, we will have to add the following dependency to the app>source>build.gradle file
implementation “com.google.android.material:material:1.3.0-alpha02”
Second, we add a shape to the button using a vector asset to app>res>drawable folder.
Third, we will add the following code to the activity_main.xml file,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:ignore="HardcodedText">
tools:context=".MainActivity"
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_baseline_add_alert_24"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/add_alert_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Add Alert"
app:layout_constraintBottom_toBottomOf="@+id/add_fab"
app:layout_constraintEnd_toStartOf="@+id/add_fab"
app:layout_constraintTop_toTopOf="@+id/add_fab" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fourth, we will add the following code to the MainActivity.java file,
MainActivity.java
package com.example.floatingactionbutton;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
FloatingActionButton mAddFab, mAddAlarmFab, mAddPersonFab;
TextView addAlarmActionText, addPersonActionText;
Boolean isAllFabsVisible;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAddFab = findViewById(R.id.add_fab);
addAlarmActionText = findViewById(R.id.add_alert_action_text);
isAllFabsVisible = false;
mAddFab.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Alert Added", Toast.LENGTH_SHORT).show();
}
});
}
}