10

So I am pretty new to android and I am trying to setup a floating action button with the Android Design Support Library using this guide in Android Studio.

My Project: Noted build.gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

My Module: app build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.noted"
        minSdkVersion 21
        targetSdkVersion 22
        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'
}

In myactivity_main.xml I try to implement

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

but I get one of those Rendering Problems pop ups after building saying:

The following classes could not be found: -android.support.design.widget.FloatingActionButton

My guess is that I'm doing something wrong with my dependencies but I really have no idea. I have been trying to get this to work for hours with no luck and would really appreciate any help.

Thanks!

Jtaks
  • 281
  • 2
  • 5
  • 13
  • Have you installed the latest versions of the "Android Support Repository" and "Android Support Library" in the Extras section of the Android SDK manager? – Sam Dozor Jun 09 '15 at 17:42
  • @SamDozor Yes I believe so. SDK manager under extras it says I have Rev 15 ASRepository and Rev 22.2 ASLibrary. – Jtaks Jun 09 '15 at 17:57
  • Can you resolve `FloatingActionButton` in your code? If you can, it must be an XML issue, not a dependency issue. – Sam Dozor Jun 09 '15 at 18:11
  • @SamDozor I am not getting an error or anything in the activity_main.xml, only when clicking on the design tab I get the "Rendering Problems" notification. I can see the ``android:src="@android:drawable/ic_input_add"`` on the rendering screen but no background. – Jtaks Jun 09 '15 at 18:48
  • Notice that this FloatingActionButton doesn't work on API 21 & higher. See this post for more information: http://stackoverflow.com/a/30609927/3922207 – Anggrayudi H Jun 09 '15 at 19:04
  • @AnggrayudiH Could you explain why it doesn't work on API 21 and higher? Your linked post only shows an alternative. Why would Google release the Android Design Support Library for all Android 2.1 or higher devices but exclude their most recent release (Lollipop)? – Jtaks Jun 09 '15 at 19:20
  • The FloatingActionButton will be displayed as rectangle image, instead of circle. So you need that class which provided by Google on Github. If you don't believe me, test it on Lollipop emulator. – Anggrayudi H Jun 10 '15 at 02:54
  • @AnggrayudiH That doesn't fix my problem of the class not being found. Thanks for the tip though! – Jtaks Jun 10 '15 at 04:51
  • have you done setContentView() for activity or inflater.inflate for fragment for the xml in which you are trying to put the floating button? – Kaveesh Kanwal Aug 03 '15 at 09:39

7 Answers7

11

You need the Library design, in the build.gradle file add:

compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:design:25.1.1'

Then Sync the Gradle, build and Reresh

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
10

I had the same issue and updated the Android Support Repository", "Android Support Library"(22.2.1) and "Android SDK Tool"(24.1.2) using SDK manager now its working fine. Once update finish rebuild the project. For more https://developer.android.com/tools/support-library/setup.html

Niroshan
  • 1,174
  • 1
  • 12
  • 30
2

I have been facing this problem recently and tried to change dependencies version, invalidating caches and restart, etc. The preview rendered once and then reopening my xml file, it raised the same error.

Clicking on the refresh button at the preview window solved to me (momentarily) the problem, letting me see the layout and its elements properly.

Preview window at xml file

Julián Martínez
  • 3,054
  • 3
  • 19
  • 17
2

The latest version of Android Studio 3.1.2 seems to not like compile in build.gradle file. Instead I used implementation and it worked fine.

implementation 'com.android.support:design:27.1.1'
Lee Harding
  • 17
  • 1
  • 3
1

If you are using androidx, you need to use:

implementation 'com.google.android.material:material:1.2.1'

(or whatever latest version you want).

The action button in the XML looks like:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/delete_buttom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="20dp"
        android:src="@drawable/ic_baseline_delete_24px" />
Mick
  • 24,231
  • 1
  • 54
  • 120
0

I had the same problem with Android Studio complaining that it couldn't instantiate FloatingActionButton. The "Exception Details" showed this:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:159)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:153)

Basically, I was using Material.Light as my theme, but I needed to use an AppCompat theme. I switched to AppCompat.Light and the problem was solved!

Amber
  • 2,413
  • 1
  • 15
  • 20
0

I had the same issue and could solve it this way : https://stackoverflow.com/a/59211828/12337593

Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67
Max Shwed
  • 232
  • 2
  • 10