0

For the layout in one of my activities, I have a floating action button on the bottom right. But for some reason, there is no padding on the margins for the button despite setting app:useCompatPadding="true".

Here is the XML code:

<?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/add_class_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:src="@drawable/ic_add_white_24dp"
        app:useCompatPadding="true" />

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</android.support.design.widget.CoordinatorLayout>

And here is an image of the layout: enter image description here

Some help would be appriecated, thanks!

Vineet Patel
  • 377
  • 6
  • 14

4 Answers4

1

Try this

<?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">


    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:layout_margin="16dp"
        android:id="@+id/add_class_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:useCompatPadding="true" />

</android.support.design.widget.CoordinatorLayout>
Rakesh R
  • 305
  • 3
  • 14
0

The reason is you are not providing margin to it. This should work

 <android.support.design.widget.FloatingActionButton
    android:contentDescription="@string/menu_compose"
    android:id="@+id/add_class_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right|end"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    app:useCompatPadding="true"
    android:src="@drawable/ic_add_white_24dp"
    app:fabSize="normal"/>
Saurabh Padwekar
  • 3,590
  • 1
  • 27
  • 36
0

refer this

app:useCompatPadding="true" 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.

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"
app:useCompatPadding="true"
...
/>

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.

BiRjU
  • 657
  • 5
  • 21
0

I Hope this will work for you.

Use as given below

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fabImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_margin="16dp"
            android:visibility="visible"
            app:backgroundTint="@color/colorAccent"
            app:borderWidth="0dp"
            app:elevation="0dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:rippleColor="#dadada"
            app:srcCompat="@mipmap/ic_image" />
GParekar
  • 1,169
  • 1
  • 7
  • 15