I am developing an Android app which doesn't require backward compatibility. Target SDK version is 22 as of now. I am using native Activity and Fragment and application theme is android:Theme.Material.Light. My problem is that I'm not able to use Snackbar with the existing setup, it throws exceptions like
android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java:640) E/AndroidRuntime(19107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
I googled but couldn't find any example of snackbar with Activity. So is it necessary to use support library like
AppCompatActivity or android.support.v4.app.Fragment.
in order to use Snackbar in my app?
You need to use the support design library compile 'com.android.support:design:23.0.1'
for it to work:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
Yes, add dependencies on your gradle
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
Then, change your app theme accordingly, you need to use the AppCompat
theme. Create the following theme on your Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
In your manifest then: add @style/AppTheme
on application and add @style/AppTheme.NoActionBar
on every activity
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".Activities.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
If you're stubborn like me and you don't want to use the support libs, but want to use the snackbar in your app, there is an option for you. I found this deprecated library that is essentially the Snackbar you know, just independent from the support libs. It works fine for me, however it might not have some features such as moving a FloatingActionButton upwards when appearing.
来源:https://stackoverflow.com/questions/34783587/how-to-use-snackbar-without-support-library