How to center align text in Extended Floating Action Button?
C

3

7

I want to center align the text in the extended floating action button but I'm not able to do that. As you can see in the image below the text is not centrally aligned.

enter image description here

Here's is the code for extended floating action button:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/compress_start_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="S"
        app:backgroundTint="?android:colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textColor="@color/textColorPrimaryDark"
        app:layout_constraintStart_toStartOf="parent"
        android:textAlignment="center"
        />

Am I doing anything wrong? Any help will be appreciated.

Centistere answered 26/2, 2021 at 15:54 Comment(0)
D
9

Please add these two lines in your ExtendedFloatingActionButton

 android:paddingStart="0dp"
 android:paddingEnd="0dp
Desirable answered 26/2, 2021 at 16:4 Comment(1)
Also needs android:gravity="center"Intercept
W
3

use like this

  <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:text="@string/ic_lin_camera" />
Warm answered 7/1, 2022 at 12:45 Comment(0)
T
0

When using an ExtendedFloatingActionButton for text only, in your layout xml for the fab you can use

style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"

or extend the above style via

<style name="extended_fab_text_only" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
    <item name="android:textAppearance">@style/myTextAppearance</item>
</style>

and you that as the style of your fab in your layout xml

style="@style/extended_fab_text_only"

Documentation: https://material.io/components/buttons-floating-action-button/android#extended-fabs Under Extended FABs > Styles:

Text-only when extended style Widget.MaterialComponents.ExtendedFloatingActionButton

Tricostate answered 19/5, 2022 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.