Skip to content

Instantly share code, notes, and snippets.

@realpacific
Last active December 31, 2018 16:16
Show Gist options
  • Save realpacific/7655cef3b9ff6685d473526fa9220ae7 to your computer and use it in GitHub Desktop.
Save realpacific/7655cef3b9ff6685d473526fa9220ae7 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/snackbar_relativelayout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:alpha="0.6"
android:layout_gravity="center_horizontal"
android:background="@android:color/black">
<TextView
android:id="@+id/snackbar_text"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:gravity="center"
android:layout_centerInParent="true"
tools:text="No connection"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@android:color/white"/>
</RelativeLayout>
//..
import com.google.android.material.snackbar.BaseTransientBottomBar
class CustomSnackbar private constructor(
parent: ViewGroup, content: View,
callback: com.google.android.material.snackbar.ContentViewCallback
) : BaseTransientBottomBar<CustomSnackbar>(parent, content, callback) {
fun setText(text: CharSequence): CustomSnackbar {
val textView = getView().findViewById<View>(R.id.snackbar_text) as TextView
textView.text = text
return this
}
/**
* content view inflation animation
*/
private class CustomContentViewCallback(private val content: View) :
com.google.android.material.snackbar.ContentViewCallback {
override fun animateContentIn(delay: Int, duration: Int) {
}
override fun animateContentOut(delay: Int, duration: Int) {
// ObjectAnimator.ofFloat(content, View.SCALE_Y, 1f, 0f).setDuration(1000).start()
}
}
companion object {
fun make(parent: ViewGroup, duration: Int): CustomSnackbar {
val inflater = LayoutInflater.from(parent.context)
val content = inflater.inflate(R.layout.custom_snackbar, parent, false)
val viewCallback = CustomContentViewCallback(content)
return CustomSnackbar(parent, content, viewCallback).run {
getView().setPadding(0, 0, 0, 0)
this.duration = duration
this
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment