Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Design Library - Floating Action Button Padding/Margin Issues

I'm using the new FloatingActionButton from the Google Design Library and I am getting some strange padding/margin problems. This image (with developer layout options on) is from API 22.

enter image description here

And from API 17.

enter image description here

This is the XML

<android.support.design.widget.FloatingActionButton         android:id="@+id/fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentEnd="true"         android:layout_gravity="bottom|right"         android:layout_marginLeft="16dp"         android:layout_marginRight="20dp"         android:layout_marginTop="-32dp"         android:src="@drawable/ic_action_add"         app:fabSize="normal"         app:elevation="4dp"         app:borderWidth="0dp"         android:layout_below="@+id/header"/> 

Why is the FAB in API 17 so much larger padding/margin wise?

like image 638
Nick H Avatar asked Jun 06 '15 21:06

Nick H


People also ask

How do you adjust a floating button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

How do you add paddings to a floating action button?

The correct solution now is to put app:useCompatPadding="true" into your FloatingActionButton. This will make the padding consistent between different API versions. However, this still seems to make the default margins off by a little bit, so you may need to adjust those.

How does padding of a view differ from the view's margin?

Cool, so margin means the space outside the view, however padding means the space inside the view.

How do you align the floating action button on flutter?

Center Align FloatingActionButton in Flutter If you are using Scaffold, you may set floatingActionButtonLocation property to FloatingActionButtonLocation. centerFloat to center make the FloatingActionButton float in the center of the screen at bottom.


1 Answers

Update (Oct 2016):

The correct solution now is to put app:useCompatPadding="true" into your FloatingActionButton. This will make the padding consistent between different API versions. However, this still seems to make the default margins off by a little bit, so you may need to adjust those. But at least there's no further need for API-specific styles.

Previous answer:

You can accomplish this easily using API-specific styles. In your normal values/styles.xml, put something like this:

<style name="floating_action_button">     <item name="android:layout_marginLeft">0dp</item>     <item name="android:layout_marginTop">0dp</item>     <item name="android:layout_marginRight">8dp</item>     <item name="android:layout_marginBottom">0dp</item> </style> 

and then under values-v21/styles.xml, use this:

<style name="floating_action_button">     <item name="android:layout_margin">16dp</item> </style> 

and apply the style to your FloatingActionButton:

<android.support.design.widget.FloatingActionButton ... style="@style/floating_action_button" ... /> 

As others have noted, in API <20, the button renders its own shadow, which adds to the overall logical width of the view, whereas in API >=20 it uses the new Elevation parameters which don't contribute to the view width.

like image 81
Dmitry Brant Avatar answered Sep 23 '22 01:09

Dmitry Brant