Skip to content

Instantly share code, notes, and snippets.

@Bencodes
Last active May 4, 2018 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bencodes/84da1018b92fe9fcb25375ff3d538903 to your computer and use it in GitHub Desktop.
Save Bencodes/84da1018b92fe9fcb25375ff3d538903 to your computer and use it in GitHub Desktop.
FloatingActionButton Android 6 Crash (using 27.1.1 of the design library)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center" />
</FrameLayout>
import android.animation.ValueAnimator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
class DemoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo)
val fab = findViewById<FloatingActionButton>(R.id.fab)
animateElevation(fab, 0F, 100F)
}
private fun animateElevation(fab: FloatingActionButton, from: Float, to: Float) {
val anim = ValueAnimator.ofFloat(0F, 1F)
anim.duration = 1000
anim.addUpdateListener {
fab.compatElevation = from + (it.animatedValue as Float) * (to - from)
}
anim.start()
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
@Bencodes
Copy link
Author

Bencodes commented May 4, 2018

DemoActivity.kt:20 will crash on Android 6 devices. The solution for us was to not animate the elevation. The setCompatElevation function works perfectly when called once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment