4

I have tried to implement floating action button in my other project with fab and customize it and it works fine. But this time, when I create the fab object in my xml layout, it shows error. Feeling confused, it tried to remove some of it's tag and found out that giving the android:backgroundTint tag is when the error shows up.

Here is the code:

    <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:fabSize="mini"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_action_fab"
    android:backgroundTint="#2196F3"
    android:layout_margin="12dp"/>

Here is my build:

compileSdkVersion 25
buildToolsVersion '25.0.0'

defaultConfig {
    applicationId "com.xxxx.xxxx"
    minSdkVersion 10
    targetSdkVersion 23
    versionCode 1
    versionName "2.0.4"
}

I have also added the google design gradle.

The error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.HomeActivity}: android.view.InflateException: Binary XML file line #141: Binary XML file line #141: Error inflating class android.support.design.widget.FloatingActionButton

As soon as i remove the android:backgroundtint tag, the error is gone. Any idea why this is happening?

user3576118
  • 375
  • 1
  • 5
  • 24

3 Answers3

10

Instead of android:backgroundTint="#2196F3", try with app:backgroundTint="#2196F3"

John Joe
  • 12,412
  • 16
  • 70
  • 135
4

According to the documentation, by default it takes the color set in styles.xml attribute colorAccent.

If you wish to change the color, in XML with attribute app:backgroundTint and not android:backgroundTint

So the final XML for fab icon will be

<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_fab"
app:backgroundTint="#2196F3"
android:layout_margin="12dp"/>
Abhi
  • 3,431
  • 1
  • 17
  • 35
1

As the answers given above, it is correct to add app:backgroundTint instead of android:backgroundTint. You can find the detail to this in this answer

Example : app:backgroundTint="@color/orange"

I don't really know if I should delete this post since it might be kind of a duplicate, but I really appreciate your help guys.

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
user3576118
  • 375
  • 1
  • 5
  • 24