Android Design Library – Floating Action Button Padding/Margin Issues –

Development issue/problem:

I am using the new FloatingActionButton from the design library of Google and I have some strange filling and branding issues. This image (with the developer’s layout options enabled) comes from API 22.

And API 17.

This is XML

Why is the FOB in API 17 so much fuller/margin?

How can this problem be solved?

Decision No 1:

Update (October 2016) :

The good solution now is to put app:useCompatPadding=true in your floating action button. This makes the coating consistent between the different versions of the PLC. However, it seems that this still reduces the standard fields a bit, so it may be necessary to adjust them. But at least the API-specific styles are no longer needed.

Earlier answer:

This can easily be done with API-specific styles. Add something like this to your normal values/styles.xml file:

and then under v21/styles.xml, use these

and apply the style to the floating action button:

As others have noticed, the button in the <20 API draws its own shadow, which contributes to the overall logical width of the view, while in the >=20 API it uses new height parameters that do not contribute to the width of the view.

Decision No 2:

No more objections for .xml or .java style files. Leave it to me.

You can use app:useCompatPadding=true and delete custom fields to keep the same fields in different versions of Android.

The extra edges/capacitors you saw on the FAB in the second image, thanks to these compatriots capacitors on devices with pre-lollipops. If this property is not defined, it applies to Pre-Loop devices, NOT to Lollipop+ devices.

Proof of concept

Decision No 3:

After several researches and tests of the solution, I solved my problem by only adding this line to my xml layout:

app: elevation=0dp
app: pressedTranslationZ=0dp

and that’s my whole floating button layout.

Decision No 4:

There is a problem in the design support library. Use the method below to solve this problem until the library is updated. Try adding this code to your activity or a fragment to solve the problem. Keep your xml unchanged. There is no margin for one or more lollipops, but there is a margin of 16dp below.

Example of update work

XML – FAB is in a relative setup

Java

FlowingActionButton mFab = (FlowingActionButton) v.findViewById(R.id.fab);
if (Build.VERSION.SDK_INT)

Convert dp to px

public static int dpToPx(context, float dp) {
// Link http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android
float scale = context.getResources().getDisplayMetrics().density;
return (int) ((dp * scale) + 0,5f);
}.

Lollipop

penultimate lollipop

Decision No 5:

The Lollipop floating action button is responsible for drawing your own shadow. So the view has to be a little bigger to make room for shade. To maintain consistent behavior, you can specify fields that take into account the difference in height and width. I’m currently using the next course:

Import android.content.context; import
android.content.res.TypedArray; import
android.support.design.widget.floatingActionButton; import
android.util.AttributeSet; import
android.viewGroup.MarginLayoutParams ;

import static android.os.Build.VERSION.SDK_INT ;
import static android.os.Build.VERSION_CODES.LOLLIPOP

import static android.support.design.R.styleable.FloatingActionButton;import static android.support.design.R.styleable.FloatingActionButton_fabSize;import static android.support.design.R.style.Widget_Design_FloatingActionButton;import static android.support.design.R.dimen.fab_size_normal;import static android.support.design.R.dimen.fab_size_mini ;

The CustomFloatingActionButton public class expands the FloatingActionButton {.

personally in mSize;

public CustomFloatingActionButton(context) {
this(context, zero);
}

public CustomFloatingActionButton(ContextContext, AttributeSet Attributes) {
this(Context, Attributes, 0);
}.

public CustomFloatingActionButton (Context, AttributeSet attrs, int defStyleAttr);typedArray a = Context.getStyledAttributes(attrs, FloatingActionButton, defStyleAttr,Widget_Design_FloatingActionButton);this.mSize = a.getInt(FloatingActionButton_fabSize, 0);a.recycle();}.

@Overrade
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (SDK_INT)

Update : Android Support Libraries v23 is renamed to fab_size dimens to :

static import android.support.design.R.dimension_fab_size_normal;
static import android.support.design.R.dimension_fab_size_mini

Decision No 6:

Marcus’ answer worked well for me after the upgrade to v23.1.0 and some import changes (with the new Gradle plugin we use our R-application instead of the R-Design library). Here is the code for v23.1.0:

// Based on this reply: https://stackoverflow.com/a/30845164/1317564
public class CustomFloatingActionButton expands the FloatingActionButton {
private int mSize ;

public CustomFloatingActionButton(context) {
this(context, zero);
}

public CustomFloatingActionButton(ContextContext, AttributeSet Attributes) {
this(Context, Attributes, 0);
}.

@SuppressLint(PrivateResource)public CustomFloatingActionButton(Context-Context, AttributeSet attrs, int defStyleAttr);TypedArray a = Context.getStyledAttributes,R.styleable.FloatingActionButton, defStyleAttr,R.style.Widget_Design_FloatingActionButton);mSize = a.getInt(R.styleable.FloatingActionButton_fabSize, 0);a.recycle();}.

@Overrade
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (Build.VERSION.SDK_INT)

Decision No 7:

In the layout file, set the height attribute to 0 because it inherits the default height.

app:elevation=0dp

Now check the Apin level above 21 in the activity and adjust the altitude if necessary.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fabBtn.setElevation(10.0f);
}

Good luck!

Related Tags:

recover deleted videos google pixel,recover data from broken google pixel,google pixel transfer broken screen,free android data recovery,google photos,google drive,how to backup pixel 3a to computer,pixel 3a waiting to backup,pixel 3a sms backup,full pixel backup,how to factory reset pixel 3a,how to backup android to pc reddit,broken android data extraction,phone data recovery,google backup,best android data recovery software,how to access internal storage on broken phone,recover deleted files pixel 2