Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.customsnackbar
- import android.animation.AnimatorSet
- import android.animation.ObjectAnimator
- import android.content.Context
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.util.AttributeSet
- import android.view.Gravity
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.view.animation.OvershootInterpolator
- import android.widget.FrameLayout
- import android.widget.LinearLayout
- import androidx.constraintlayout.widget.ConstraintLayout
- import androidx.core.content.ContextCompat
- import androidx.core.view.updateLayoutParams
- import com.google.android.material.snackbar.BaseTransientBottomBar
- import com.google.android.material.snackbar.ContentViewCallback
- import com.google.android.material.snackbar.Snackbar
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- textView.setOnClickListener {
- CustomSnackBar.make(textView.parent as ViewGroup, Snackbar.LENGTH_LONG, Gravity.BOTTOM).show()
- }
- }
- }
- class CustomSnackBar(parent: ViewGroup,
- content: View,
- gravity: Int,
- contentViewCallback: com.google.android.material.snackbar.ContentViewCallback
- ) : BaseTransientBottomBar<CustomSnackBar>(parent, content, contentViewCallback) {
- init {
- getView().setPadding(0, 0, 0, 0)
- val layoutParams = getView().layoutParams as ConstraintLayout.LayoutParams
- layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
- layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
- layoutParams.width = FrameLayout.LayoutParams.WRAP_CONTENT
- if (gravity == Gravity.BOTTOM) {
- layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
- }
- getView().layoutParams = layoutParams
- getView().setBackgroundResource(R.drawable.bg_toast)
- }
- companion object {
- fun make(parent: ViewGroup, duration: Int, gravity: Int): CustomSnackBar {
- val content = CustomSnackBarView(parent.context)
- val customSnackBar = CustomSnackBar(parent, content, gravity, content)
- customSnackBar.duration = duration
- return customSnackBar
- }
- }
- }
- class CustomSnackBarView @JvmOverloads constructor(
- context: Context,
- attrs: AttributeSet? = null,
- defStyleAttr: Int = 0
- ) : LinearLayout(context, attrs, defStyleAttr), ContentViewCallback {
- override fun animateContentOut(p0: Int, p1: Int) {
- // val scaleX = ObjectAnimator.ofFloat(this, View.ALPHA, 1f, 0f)
- // scaleX.duration = 500
- // scaleX.start()
- }
- override fun animateContentIn(p0: Int, p1: Int) {
- // val scaleX = ObjectAnimator.ofFloat(this, View.ALPHA, 0f, 1f)
- // scaleX.duration = 500
- // scaleX.start()
- }
- init {
- val inflater = LayoutInflater.from(context)
- inflater.inflate(R.layout.layout_snackbar, this, true)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement