42

I would like use a FloatingActionButton on my application, I read this : https://guides.codepath.com/android/Floating-Action-Buttons#google-s-official-support-library but when I run the Activity I have this error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xavier_laffargue.podcast/com.xavier_laffargue.podcast.ACT_Test}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout

XML File

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lvToDoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"
        android:src="@drawable/ic_action_refresh"
        app:layout_anchor="@id/lvToDoList"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

Graddle

*apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.xavier_laffargue.podcast"
        minSdkVersion 21
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'

    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-v13:22.2.0'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'

}
xav-stargate
  • 677
  • 1
  • 5
  • 16

13 Answers13

47

For those using AndroidX Dependency

Along with changing dependencies, XML must also be changed.

from

<android.support.design.widget.CoordinatorLayout 

to

<androidx.coordinatorlayout.widget.CoordinatorLayout
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
41

Please change it into AppCompatActivity if you use Activity. Probably it becomes the error when it is Activity.

Mizuki Sonoko
  • 544
  • 6
  • 8
26

Just use this line in FloatingActionButton (app instead of android):

app:backgroundTint="@color/colorAccent"
passsy
  • 5,162
  • 4
  • 39
  • 65
Preet Jay
  • 309
  • 4
  • 8
18

Please put compile 'com.android.support:design:23.0.1' inside your project build.gradle file

David Corral
  • 4,085
  • 3
  • 26
  • 34
rakesh rajput
  • 606
  • 4
  • 5
  • This is the logical answer ! – Umair Oct 04 '16 at 15:50
  • 1
    I believe this is the most often missed issue as it doesn't appear in most other answers nor examples. The answer above mentions both appcompat and design, so is technically more correct, but I bet most people wouldn't get this far if appcompat were missing. The actual version of design doesn't matter, so feel free to use a newer one if your Android Studio has a newer one already installed. – Evan Langlois Oct 21 '16 at 20:55
  • now google play say it must be 28 min, and it did not solve the problem – shareef Jan 03 '19 at 07:33
16

I had the same error. Just change the project parent theme to

<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

or any other Theme.AppCompat*

ibrik
  • 368
  • 2
  • 12
6

You have to include the support libraries.

  1. Go to "Project Structure" -> Dependencies
  2. On the right side click "+" and select "1. Library dependency"
  3. Search for "android.support"
  4. Add both:
    • com.android.support:appcompat-v7:.......
    • com.android.support:design:........
  5. Sync Gradle enter image description here Happy codding! :)
Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
4

needed both:

  • extend AppCompatActivity instead of FragmentActivity

    public class MyActivity extends AppCompatActivity
    
  • parent of used style (/res/values/styles.xml)

    <style name="MyStyle" parent="Theme.AppCompat">
    

additionally:

  • define colors in styles.xml

    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    
electrobabe
  • 1,549
  • 18
  • 17
4

Add these dependencies into your gradle file. This may solve the problem in some cases.

dependencies {
   compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:design:23.1.1'
   ...
}
CrazyTN
  • 19
  • 6
FN90
  • 421
  • 4
  • 13
3

For Xamarin Developers:

(supports Android API 7 to 22+)

  1. Make sure you have installed following components:
    • Android Support Design Library
    • Android Support Library v7 AppCompat
  2. Make sure all NuGet packages for these components are installed and referenced. These are:
    • Xamarin.Android.Support.Design
    • Xamarin.Android.Support.v4
    • Xamarin.Android.Support.v7.AppCompat
    • Xamarin.Android.Support.v7.RecyclerView
  3. Your Apps Activity should derive from Android.Support.V7.App.AppCompatActivity
  4. Your used style has to be derived from a Theme.AppCompat.* style. So your Resources\values\styles.xml should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="YourAppTheme" parent="Theme.AppCompat">
            <item name="colorPrimaryDark">#AB000D</item>
            <item name="colorPrimary">#E53935</item>
            <item name="colorAccent">#00B8D4</item>
        </style>
        <!-- other styles... -->
    </resources>
    
  5. Also make sure you actually use the theme in your AndroidManifest.xml

    <application android:theme="@style/YourAppTheme"></application>
    
Bruno Zell
  • 7,761
  • 5
  • 38
  • 46
1

You need to add in your app's build gradle the following support library.

    compile 'com.android.support:design:23.0.1'

that was last year, now the latest version is

    compile 'com.android.support:design:27.0.2'
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Yosidroid
  • 2,053
  • 16
  • 15
0

For me, I came across this error when using a mix of androidx and android.support.v7 libraries.

See my solution for that version of this error here: https://stackoverflow.com/a/52490825/1762493

Mikeumus
  • 3,570
  • 9
  • 40
  • 65
0

For those using AndroidX Dependency. In your xml files make sure all your

 android.support.???

nodes inside CoordinatorLayout's (like included ActionBars) are also replaced with

androidx.??? (or com.google.android.???)

ones .

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
-1

It worked for me

Disable Instant Run

File => Settings => Build, Execution, Deployment => Instant Run

KamDroid
  • 102
  • 4
  • 7