FAB with anchor in coordinator layout has extra margin in android pre-lollipop

10,716

Solution 1

I dont think you want to place them without margins. If I understood it right, you've done this to see whats going on in different versions of android.

You can use app:useCompatPadding="true" and remove custom margins to maintain same margins across different versions of android

android studio code

Proof of concept

design view

Solution 2

According to this link, it seems to be a bug in android design library. It says that:

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.

So I have to provide two resource files for margin:

res/values:

<dimen name= "fab_margin_right">0dp</dimen>

And in res/values-v21:

<dimen name = "fab_margin_right">8dp</dimen>

Solution 3

Since 22.2.1 version of support and design library, previous answer is no longer true. There is no extra padding if FAB is inside CoordinatorLayout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_show_qr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@mipmap/ic_action_edit"
        app:backgroundTint="@color/primary"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal"
        app:rippleColor="@color/primary_dark"/>
</android.support.design.widget.CoordinatorLayout>

This code will produce following FAB on every Android version.

enter image description here

Share:
10,716
Misagh Emamverdi
Author by

Misagh Emamverdi

Updated on July 27, 2022

Comments

  • Misagh Emamverdi
    Misagh Emamverdi almost 2 years

    I have a CoordinatroLayoutwith FloatingActionButton. Here is my code:

    <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar_layout"
            android:layout_above="@+id/actionbar">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:minHeight="?android:attr/actionBarSize"
                    android:background="@color/toolbar_color" />
    
    
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
    
                    >
    
                </ScrollView>
    
    
            </LinearLayout>
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:clickable="true"
                app:fabSize="mini"
                android:src="@mipmap/ic_action_edit"
                app:layout_anchor="@id/toolbar"
                app:layout_anchorGravity="bottom|right|end"
                app:backgroundTint="@color/toolbar_color"            />
            </android.support.design.widget.CoordinatorLayout>
    

    But it appears differently in lollipop and pre-lollipop devices.

    Lollipop:

    enter image description here

    Pre-Lollipop: enter image description here

    Actually I haven't add any margin. But FAB has margin in pre-lollipop devices.

    I have also seen this problem in cheessesquare sample. It shows different margins too. What is the problem?

  • Misagh Emamverdi
    Misagh Emamverdi over 8 years
    Thank you but I was using support library 23.0.0 when I faced the problem.
  • Saravanabalagi Ramachandran
    Saravanabalagi Ramachandran about 8 years
    I still have no clue why no other answer has described the same. I had the same problem, and now its fixed :)
  • Misagh Emamverdi
    Misagh Emamverdi almost 8 years
    Thank you! I have changed the selected answer.
  • Amir Ziarati
    Amir Ziarati about 7 years
    but there is if its in another layout like linear and etc :/ android is silly and an insult to developers