Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Action Button Icon not centered inside

I'm trying to use a FAB but the icon inside is placing at bottom right of the button:

FAB with icon not centered

This is the FAB definition in the xml:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/centerLocationButton"
            style="@style/Widget.MaterialComponents.FloatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_margin="@dimen/pro_arriving_map_location_button_padding"
            android:src="@drawable/ic_my_location"
            app:backgroundTint="@color/white"
            app:fabSize="mini" />

    </RelativeLayout>

What am I doing wrong?

like image 387
Gastón Sanguinetti Avatar asked Oct 03 '18 20:10

Gastón Sanguinetti


People also ask

Why can't I change the background image of the Floating Action Button?

Reason is your image is not in the right size!! Floating action button default circle size is 56 x 56dp , to get the best fit use that for your background image!! If you only want to change the Interior icon (only icon) use a 24 x 24dp icon for default size

What is a Floating Action Button?

A floating action button (FAB) is a circular button that triggers the primary action in your app's UI.

Why are my Floating buttons not working in my layout?

This could happen because the layout style is overriding the floating button style. To solve it make sure to reestablish the style defined as your own like this:

How to change icon size layout width & height for navigation button?

Here it is about setting three options together to adjust the navigation button with icon size layout width & height. change icon size inside the nav.Button ~ Using (maxImageSize="..dp"). change fab size ~ Using (app:fabCustomSize="..dp") OR (app:fabSize="") You can try three options here ("mini" or "auto" or "normal"). For example:


Video Answer


2 Answers

This seems to be a bug in the Design Support Library v28.0.0.

I could workarround this by setting the scaleType programmatically.

In your case in the Java/Kotlin Code:

centerLocationButton.setScaleType(ImageView.ScaleType.CENTER)
like image 50
mikone Avatar answered Oct 01 '22 07:10

mikone


If you're using material design and using custom sizes:

android:layout_width="80dp"
android:layout_height="80dp"

instead of

android:layout_width="wrap_content"
android:layout_height="wrap_content"

The fix is to use these two properties

app:fabCustomSize="80dp"
app:maxImageSize="70dp"

make sure of this

app:fabCustomSize = the same size as your layout_width
app:maxImageSize= about 10dp minus the size of your layout_width
like image 35
Neb King Avatar answered Oct 01 '22 07:10

Neb King