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?
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
A floating action button (FAB) is a circular button that triggers the primary action in your app's UI.
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:
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:
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With