Tutorials  /  JavaScript

The Art of User Guidance: A Guide to the Android Floating Action Button

Ccentron Redaktion · July 2024 ·3 min read ·JavaScript, Tutorial

Discover the secrets of effective app design with our latest blog post! Dive into the world of the Android Floating Action Button and learn how you can revolutionize user interaction with SnackBars. Get inspired and optimize your app user interface today!

What is the Android Floating Action Button?

The Android Floating Action Button is used to highlight key functions on the screen. It's a stylish and effective method to draw users' attention.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.11 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Overview of the Android Floating Action Button

To use Material Design widgets in our project, we need to add the following dependency in our build.gradle file:

Code
compile 'com.android.support:design:23.1.1'

The FloatingActionButton is defined in XML layout as follows:

<android.support.design.widget.FloatingActionButton

      android:id="@+id/fab"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:src="@android:drawable/ic_dialog_email"

      android:layout_gravity="bottom|end"

      app:elevation="6dp"

      app:pressedTranslationZ="12dp"/>

Code

Some observations from the above XML layout are:

  • The FloatingActionButton extends the ImageView class, as evident by the android:src attribute.
  • The elevation attribute in the above XML layout is used to cast a shadow over the button, and pressedTranslationZ causes the shadow to grow when pressed.

A FloatingActionButton is placed within a CoordinatorLayout. A CoordinatorLayout facilitates interactions between the contained views and is useful for animating the button based on scroll changes.

SnackBar: The Better Alternative to Toasts

SnackBar is an evolved widget compared to Toasts. A SnackBar is called as follows:

Code
Snackbar.make(view, "Replace this with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();

Project Structure for the Android Floating Action Button Example

The default project structure includes a new XML layout file named content_main.xml, which corresponds to the previous activity_main.xml.

The new activity_main.xml by default includes a Toolbar as a replacement for an ActionBar. It's added within an AppBarLayout, which is a direct child of the CoordinatorLayout. The AppBarLayout is used to achieve various scrolling behaviors like collapsing, flex space, and quick return.

Final Thoughts

The Android Floating Action Button is a powerful tool to enhance user experience in your apps. When combined with SnackBars, they provide an elegant way to highlight important functions and facilitate user interactions.

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.