Bart Friederichs
Bart Friederichs

Reputation: 33491

How to use a Snackbar with AndroidX?

I am trying to add a Snackbar to my app,

Snackbar.make(findViewById(android.R.id.content), 
            R.string.not_found, Snackbar.LENGTH_INDEFINITE).show();

but run into this error message:

10-05 16:48:24.530 28492-28492/nl.minerall.sapphire.pocket.full E/AndroidRuntime: FATAL EXCEPTION: main
    Process: nl.minerall.sapphire.pocket.full, PID: 28492
    android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:188)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:230)

I Googled some and it tells me I have to add com.android.support:design to my build.gradle, but this completely breaks my build, as I am using AndroidX:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'   
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.google.android:flexbox:1.1.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'

    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}

Update I poked a little around and found this XML file that is being inflated (/extras/android/support/design/res/layout/design_layout_snackbar_include.xml) :

<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2015 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
            android:id="@+id/snackbar_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingTop="@dimen/design_snackbar_padding_vertical"
            android:paddingBottom="@dimen/design_snackbar_padding_vertical"
            android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
            android:paddingRight="@dimen/design_snackbar_padding_horizontal"
            android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
            android:maxLines="@integer/design_snackbar_text_max_lines"
            android:layout_gravity="center_vertical|left|start"
            android:ellipsize="end"
            android:textAlignment="viewStart"/>

    <Button
            android:id="@+id/snackbar_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
            android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
            android:layout_gravity="center_vertical|right|end"
            android:paddingTop="@dimen/design_snackbar_padding_vertical"
            android:paddingBottom="@dimen/design_snackbar_padding_vertical"
            android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
            android:paddingRight="@dimen/design_snackbar_padding_horizontal"
            android:visibility="gone"
            android:textColor="?attr/colorAccent"
            style="?attr/borderlessButtonStyle"/>

</merge>

When just copying that file into my project and opening it in Android Studio, there seems to be something wrong with ?attr/colorAccent, because when I remove that, the designer shows no errors. But, how to fix all this ... ?

Upvotes: 4

Views: 6210

Answers (3)

Leonardo Sibela
Leonardo Sibela

Reputation: 2179

What you should use is this:

implementation "com.google.android.material:material:1.1.0-alpha06"

If you're aware on how to configure your dependency in the support way, you can check the Android Developers page on Migrating to AndroidX . Also, check the most current version, which will probably not be 1.1.0-alpha06 by the point you are reading this.

Upvotes: 2

Saksham Dubey
Saksham Dubey

Reputation: 134

I added to my build.gradle(Module: app)

implementation 'com.android.support:design:28.0.0'

It works fine and my gradle builds successfully.

These are my dependencies:-

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android:flexbox:1.1.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'com.android.support:design:28.0.0'

}

Upvotes: -1

Slick Slime
Slick Slime

Reputation: 693

You need to import

com.google.android.material.snackbar.Snackbar

instead of

android.support.design.widget.Snackbar

Upvotes: 1

Related Questions