26

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?

Virendra Varma
  • 895
  • 1
  • 12
  • 23

3 Answers3

43

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
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • But how do you get the button to appear at the bottom right and not at the top left as it does with this code? – Richard Barraclough Jun 10 '20 at 11:30
  • @RichardBarraclough didn't get you? – AskNilesh Jun 10 '20 at 11:31
  • I tried the code above and the button appears, but it's at the top left of the screen. – Richard Barraclough Jun 10 '20 at 20:56
  • @RichardBarraclough your button appears at top left of the screen because I have added `app:layout_constraintBottom_toBottomOf` `app:layout_constraintEnd_toEndOf`, `app:layout_constraintStart_toStartOf` , `app:layout_constraintTop_toTopOf` just remove this from your code and apply constraint as per you requirement – AskNilesh Jun 11 '20 at 02:45
9

Now you can migrate to Android x and replace the xml with:

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

Also in the gradle file add:

implementation 'com.google.android.material:material:1.0.0'
Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
-1

To use Floating action button in androidx library use this implementation

implementation 'com.google.android.material:material:1.0.0'
Abra
  • 19,142
  • 7
  • 29
  • 41
Nageshwor Shah
  • 101
  • 1
  • 1
  • 8