[android] Adjust icon size of Floating action button (fab)

Floating button The new floating action button should be 56dp x 56dp and the icon inside it should be 24dp x 24dp. So the space between icon and button should be 16dp.

<ImageButton
    android:id="@+id/fab_add"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:background="@drawable/ripple_oval"
    android:elevation="8dp"
    android:src="@drawable/ic_add_black_48dp" />

ripple_oval.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

And this is the result I get:
Floating button result
I used the icon from \material-design-icons-1.0.0\content\drawable-hdpi\ic_add_black_48dp.png
https://github.com/google/material-design-icons/releases/tag/1.0.1

How to make the size of the icon inside the button be exactly as described in guidelines ?

http://www.google.com/design/spec/components/buttons.html#buttons-floating-action-button

The answer is


Make this entry in dimens

<!--Floating action button-->
<dimen name="design_fab_image_size" tools:override="true">36dp</dimen>

Here 36dp is icon size on floating point button. This will set 36dp size for all icons for floating action button.

Updates As Per Comments

If you want to set icon size to particular Floating Action Button just go with Floating action button attributes like app:fabSize="normal" and android:scaleType="center".

  <!--app:fabSize decides size of floating action button You can use normal, auto or mini as per need-->
  app:fabSize="normal" 

  <!--android:scaleType decides how the icon drawable will be scaled on Floating action button. You can use center(to show scr image as original), fitXY, centerCrop, fitCenter, fitEnd, fitStart, centerInside.-->
  android:scaleType="center"

<ImageButton
        android:background="@drawable/action_button_bg"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:padding="16dp"
        android:src="@drawable/ic_add_black_48dp"
        android:scaleType="fitXY"
        android:elevation="8dp"/>

With the background you provided it results in below button on my device (Nexus 7 2012)

enter image description here

Looks good to me.


You can play with the following settings of FloatingActionButton : android:scaleType and app:maxImageSize. As for me, I've got a desirable result if android:scaleType="centerInside" and app:maxImageSize="56dp".


You can use iconSize like this:

    floatingActionButton: FloatingActionButton(
      onPressed: () {
        // Add your onPressed code here
      },
      child: IconButton(
        icon: isPlaying
            ? Icon(
                Icons.pause_circle_outline,
              )
            : Icon(
                Icons.play_circle_outline,
              ),
              iconSize: 40,
              
        onPressed: () {
          setState(() {
            isPlaying = !isPlaying;
          });
        },
       
      ),
    ),

As FAB is like ImageView so You can use scaleType attribute to change the icon size likewise ImageView. Default scaleType for a FAB is fitCenter. You can use center and centerInside to make icon small and large respectively.

All values for scaleType attribute are - center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY and matrix.

See https://developer.android.com/reference/android/widget/ImageView.ScaleType.html for more details.


There are three key XML attributes for custom FABs:

  • app:fabSize: Either "mini" (40dp), "normal"(56dp)(default) or "auto"
  • app:fabCustomSize: This will decide the overall FAB size.
  • app:maxImageSize: This will decide the icon size.

Example:

app:fabCustomSize="64dp" 
app:maxImageSize="32dp"

The FAB padding (the space between the icon and the background circle, aka ripple) is calculated implicitly by:

4-edge padding = (fabCustomSize - maxImageSize) / 2.0 = 16

Note that the margins of the fab can be set by the usual android:margin xml tag properties.


What's your goal?

If set icon image size to bigger one:

  1. Make sure to have a bigger image size than your target size (so you can set max image size for your icon)

  2. My target icon image size is 84dp & fab size is 112dp:

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src= <image here>
        app:fabCustomSize="112dp"
        app:fabSize="auto"
        app:maxImageSize="84dp" />
    

Try to use app:maxImageSize="56dp" instead of the above answers after you update your support library to v28.0.0


off of @IRadha's answer in vector drawable setting android:height="_dp" and setting the scale type to android:scaleType="center" sets the drawable to whatever size was set


If you are using androidx 1.0.0 and are using a custom fab size, you will have to specify the custom size using

app:fabCustomSize="your custom size in dp"

By deafult the size is 56dp and there is another variation that is the small sized fab which is 40dp, if you are using anything you will have to specify it for the padding to be calculated correctly


The design guidelines defines two sizes and unless there is a strong reason to deviate from using either, the size can be controlled with the fabSize XML attribute of the FloatingActionButton component.

Consider specifying using either app:fabSize="normal" or app:fabSize="mini", e.g.:

<android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_done_white_24px"
   app:fabSize="mini" />

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to material-design

Flutter: Setting the height of the AppBar How to use the new Material Design Icon themes: Outlined, Rounded, Two-Tone and Sharp? Selected tab's color in Bottom Navigation View Material UI and Grid system How to set a ripple effect on textview or imageview on Android? How can a divider line be added in an Android RecyclerView? How to show Snackbar when Activity starts? How to change the new TabLayout indicator color and height Android Design Support Library expandable Floating Action Button(FAB) menu Android Support Design TabLayout: Gravity Center and Mode Scrollable

Examples related to floating-action-button

Android changing Floating Action Button color Android Design Support Library expandable Floating Action Button(FAB) menu FloatingActionButton example with Support Library Adjust icon size of Floating action button (fab) How can I add the new "Floating Action Button" between two widgets/layouts

Examples related to material-components-android

Toolbar overlapping below status bar Adjust icon size of Floating action button (fab) Material Design not styling alert dialogs

Examples related to android-icons

How to import set of icons into Android Studio project Adjust icon size of Floating action button (fab) How to change option menu icon in the action bar? Android - Launcher Icon Size How to change the icon of an Android app in Eclipse? Standard Android menu icons, for example refresh