Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Theming Floating Action Buttons in Android with Example

Theming the FloatingActionButton (FAB) primarily involves changing its background color, ripple color, and icon. Here's a step-by-step guide to theme a FloatingActionButton in Android:

1. XML Layout:

Declare your FloatingActionButton in your XML layout:

activity_main.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:contentDescription="@string/fab_desc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. Theming the FAB:

You can theme the FAB directly in XML:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    ...
    app:backgroundTint="@color/yourColor"
    app:rippleColor="@color/yourRippleColor"
    app:tint="@color/yourIconColor"
/>

Replace yourColor, yourRippleColor, and yourIconColor with your desired colors.

3. Theming the FAB Programmatically:

Alternatively, you can also theme the FAB programmatically:

MainActivity.java:

import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = findViewById(R.id.fab);

        // Set background color
        fab.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE));

        // Set ripple color
        fab.setRippleColor(Color.RED);

        // Set icon color
        fab.setImageTintList(ColorStateList.valueOf(Color.WHITE));
    }
}

Don't forget to import android.content.res.ColorStateList.

Note:

To use the FloatingActionButton, ensure you have the Material Components library in your build.gradle:

implementation 'com.google.android.material:material:<version>'

Replace <version> with the appropriate library version. Always ensure you're using the latest stable version.

Finally, to leverage Material Components fully, make sure your app's theme inherits from a Material Components theme (e.g., Theme.MaterialComponents.Light.DarkActionBar).

  1. Customizing Floating Action Button appearance in Android:

    • Description: Demonstrates the basic customization of a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          app:backgroundTint="@color/colorAccent"
          app:borderWidth="0dp"
          app:elevation="6dp"
          app:fabSize="normal"
          app:rippleColor="@color/colorAccentDark"
          app:layout_anchor="@id/bottomAppBar"
          app:layout_anchorGravity="bottom|end" />
      
  2. Changing color and style of Floating Action Button in Android:

    • Description: Illustrates changing the color and style of a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          app:backgroundTint="@color/customFabColor"
          app:borderWidth="2dp"
          app:elevation="8dp"
          app:fabSize="mini"
          app:rippleColor="@color/customFabRippleColor"
          app:layout_anchor="@id/bottomAppBar"
          app:layout_anchorGravity="bottom|end" />
      
  3. Material Design theming for Floating Action Button Android:

    • Description: Integrates Material Design theming for a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          style="@style/Widget.MaterialComponents.FloatingActionButton"
          ... />
      
  4. Using styles and themes with FloatingActionButton in Android:

    • Description: Demonstrates the use of styles and themes for a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          style="@style/MyFloatingActionButton"
          ... />
      
      <!-- styles.xml -->
      <style name="MyFloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton">
          <item name="backgroundTint">@color/customFabColor</item>
          <item name="borderWidth">2dp</item>
          <!-- Add other style attributes as needed -->
      </style>
      
  5. Adding icon and text to Floating Action Button with theming:

    • Description: Adds an icon and text to a themed Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          app:backgroundTint="@color/colorAccent"
          app:borderWidth="0dp"
          app:elevation="6dp"
          app:fabSize="normal"
          app:rippleColor="@color/colorAccentDark"
          app:layout_anchor="@id/bottomAppBar"
          app:layout_anchorGravity="bottom|end"
          app:labelText="Add" />
      
  6. Floating Action Button animations and theming in Android:

    • Description: Introduces animations and theming for a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          style="@style/Widget.MaterialComponents.FloatingActionButton.Extended"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          android:contentDescription="@string/add"
          android:text="@string/add"
          app:backgroundTint="@color/colorAccent"
          app:rippleColor="@color/colorAccentDark"
          app:layout_anchor="@id/bottomAppBar"
          app:layout_anchorGravity="bottom|end" />
      
  7. Themed Floating Action Button with CoordinatorLayout in Android:

    • Description: Demonstrates using a CoordinatorLayout with a themed Floating Action Button.
    • Example Code (XML):
      <androidx.coordinatorlayout.widget.CoordinatorLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <com.google.android.material.floatingactionbutton.FloatingActionButton
              android:id="@+id/fab"
              style="@style/Widget.MaterialComponents.FloatingActionButton"
              ... />
      
          <!-- Add other layout elements as needed -->
      
      </androidx.coordinatorlayout.widget.CoordinatorLayout>
      
  8. Applying custom colors to FloatingActionButton Android:

    • Description: Applies custom colors to a Floating Action Button in Android.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          app:backgroundTint="@color/customFabBackground"
          app:rippleColor="@color/customFabRippleColor"
          ... />
      
  9. Floating Action Button theming in XML layout in Android:

    • Description: Specifies Floating Action Button theming directly in XML layout.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          style="@style/Widget.MaterialComponents.FloatingActionButton"
          ... />
      
  10. Changing size and elevation of FloatingActionButton with theming:

    • Description: Adjusts the size and elevation of a themed Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="56dp"
          android:layout_height="56dp"
          android:src="@drawable/ic_add"
          app:elevation="12dp"
          ... />
      
  11. Floating Action Button states and theming in Android:

    • Description: Addresses different states and theming of a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          style="@style/Widget.MaterialComponents.FloatingActionButton"
          app:backgroundTint="@color/customFabColorStateList"
          ... />
      
  12. Custom drawable for FloatingActionButton in Android theming:

    • Description: Utilizes a custom drawable for theming a Floating Action Button.
    • Example Code (XML):
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:srcCompat="@drawable/custom_fab_icon"
          ... />
      
  13. Creating themed Floating Action Button with Kotlin in Android:

    • Description: Creates a themed Floating Action Button using Kotlin.
    • Example Code (Kotlin):
      val fab: FloatingActionButton = findViewById(R.id.fab)
      fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.customFabColor))
      fab.setImageResource(R.drawable.ic_add)