Search code examples
androidfloating-action-buttonandroidx

Floating action button in andoidx library


Hi is there anyone knows how to use the floating action button with andoidx library I think in android x library they replace

implementation 'com.android.support:appcompat-v7:xx.xx.xx'

by

implementation 'androidx.appcompat:appcompat:1.0.0'

but it's not helping any suggetion?


Solution

  • To use Floating action button in andoidx library use below dependencies

    implementation 'com.google.android.material:material:1.0.0-rc01'
    

    For more information check Migrating to AndroidX

    Complete example

    XML code

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/imgFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_close"
            app:backgroundTint="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@android:color/white" />
    

    Notes

    app:tint="@android:color/white" is used to change the icon color of FloatingActionButton

    app:backgroundTint="@color/colorAccent" is used to change background color of FloatingActionButton

    To use FloatingActionButton inside activity or fragment we need to import FloatingActionButton like this

    import com.google.android.material.floatingactionbutton.FloatingActionButton