Last active
May 4, 2018 05:43
-
-
Save Bencodes/84da1018b92fe9fcb25375ff3d538903 to your computer and use it in GitHub Desktop.
FloatingActionButton Android 6 Crash (using 27.1.1 of the design library)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.