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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

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.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren