Search code examples
androidalertmessageandroid-snackbar

Error with method make and symbol LENGTH_LONG in Snackbar


I'm trying to show a snackbar

Snackbar.make(view.findViewById(android.R.id.content), "Message", Snackbar.LENGTH_LONG).show();

But there are two error I don't know why?
1.

Cannot resolve method 'make(android.view.View, java.lang.String, ?)'

2.

Cannot resolve symbol 'LENGTH_LONG'

enter image description here Can anyone tell me why these errors occur?

UPDATE
build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.domain.app"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.nispok:snackbar:2.6.1'
}

Solution

  • This is the correct way to go for:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.1"
    
        defaultConfig {
            applicationId "com.domain.app"
            minSdkVersion 10
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:design:25.1.0'
    }
    

    I've also changed the compileSdkVersion & buildToolsVersion to the latest version as its best to choose the bleeding edge code. If you do prefer , you can revert to the old one as well. But DO note that always keep compileSdkVersion & buildToolsVersion to be same, ie If you are choosing 23 choose both to be 23 , else it would cause issues.

    Now, coming to the SnackBar, SnackBar is part of Android's design library. And you were compiling compile 'com.nispok:snackbar:2.6.1' . Instead use the design library: compile 'com.android.support:design:25.1.0' . That should resolve the issue.

    Now , from your Activity, invoke the SnackBar this way:

    Snackbar.make(this.findViewById(android.R.id.content), "Message", Snackbar.LENGTH_LONG).show()