Search code examples
androidfloating-action-button

In Android studio how to let a image in FloatingActionButton center


enter image description here

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:contentDescription="TODO"
    android:focusable="true"
    android:scaleType="fitCenter"
    app:backgroundTint="@color/black"
    app:fabSize="auto"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/checkBox6"
    app:maxImageSize="100dp"
    app:srcCompat="@drawable/add"
    app:tint="@color/blue" />

I want to make the picture center in Button, I tried to use constrained layout, but it didn't work in the inner of the FloatingActionButton. I chang my code but it didn't work.

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="60dp"
    android:clickable="true"
    app:backgroundTint="#000000"
    app:fabCustomSize="50dp"
    app:elevation="0dp"
    app:borderWidth="0dp"
    app:fabSize="normal"
    app:pressedTranslationZ="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:maxImageSize="100dp"
    app:srcCompat="@drawable/add"
    app:tint="@color/blue" />

<com.google.android.material.floatingactionbutton.FloatingActionButton

enter image description here


Solution

  • <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/fab"
                app:backgroundTint="@color/colortoolbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/fab_margin"
                app:srcCompat="@drawable/world"
                app:fabSize="mini"/>
    

    You are giving different margin sizes.remove them.