SHARE
TWEET

Untitled

a guest Nov 12th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.customsnackbar
  2.  
  3. import android.animation.AnimatorSet
  4. import android.animation.ObjectAnimator
  5. import android.content.Context
  6. import androidx.appcompat.app.AppCompatActivity
  7. import android.os.Bundle
  8. import android.util.AttributeSet
  9. import android.view.Gravity
  10. import android.view.LayoutInflater
  11. import android.view.View
  12. import android.view.ViewGroup
  13. import android.view.animation.OvershootInterpolator
  14. import android.widget.FrameLayout
  15. import android.widget.LinearLayout
  16. import androidx.constraintlayout.widget.ConstraintLayout
  17. import androidx.core.content.ContextCompat
  18. import androidx.core.view.updateLayoutParams
  19. import com.google.android.material.snackbar.BaseTransientBottomBar
  20. import com.google.android.material.snackbar.ContentViewCallback
  21. import com.google.android.material.snackbar.Snackbar
  22. import kotlinx.android.synthetic.main.activity_main.*
  23.  
  24. class MainActivity : AppCompatActivity() {
  25.  
  26.     override fun onCreate(savedInstanceState: Bundle?) {
  27.         super.onCreate(savedInstanceState)
  28.         setContentView(R.layout.activity_main)
  29.  
  30.         textView.setOnClickListener {
  31.             CustomSnackBar.make(textView.parent as ViewGroup, Snackbar.LENGTH_LONG, Gravity.BOTTOM).show()
  32.         }
  33.  
  34.     }
  35. }
  36.  
  37. class CustomSnackBar(parent: ViewGroup,
  38.                      content: View,
  39.                      gravity: Int,
  40.                      contentViewCallback: com.google.android.material.snackbar.ContentViewCallback
  41. ) : BaseTransientBottomBar<CustomSnackBar>(parent, content, contentViewCallback) {
  42.  
  43.     init {
  44.         getView().setPadding(0, 0, 0, 0)
  45.         val layoutParams = getView().layoutParams as ConstraintLayout.LayoutParams
  46.         layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
  47.         layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
  48.         layoutParams.width = FrameLayout.LayoutParams.WRAP_CONTENT
  49.  
  50.         if (gravity == Gravity.BOTTOM) {
  51.             layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
  52.         }
  53.  
  54.         getView().layoutParams = layoutParams
  55.         getView().setBackgroundResource(R.drawable.bg_toast)
  56.     }
  57.  
  58.     companion object {
  59.         fun make(parent: ViewGroup, duration: Int, gravity: Int): CustomSnackBar {
  60.             val content = CustomSnackBarView(parent.context)
  61.             val customSnackBar = CustomSnackBar(parent, content, gravity, content)
  62.             customSnackBar.duration = duration
  63.  
  64.             return customSnackBar
  65.         }
  66.     }
  67. }
  68.  
  69. class CustomSnackBarView @JvmOverloads constructor(
  70.     context: Context,
  71.     attrs: AttributeSet? = null,
  72.     defStyleAttr: Int = 0
  73. ) : LinearLayout(context, attrs, defStyleAttr), ContentViewCallback {
  74.     override fun animateContentOut(p0: Int, p1: Int) {
  75. //        val scaleX = ObjectAnimator.ofFloat(this, View.ALPHA, 1f, 0f)
  76. //        scaleX.duration = 500
  77. //        scaleX.start()
  78.     }
  79.  
  80.     override fun animateContentIn(p0: Int, p1: Int) {
  81.     //        val scaleX = ObjectAnimator.ofFloat(this, View.ALPHA, 0f, 1f)
  82.     //        scaleX.duration = 500
  83.     //        scaleX.start()
  84.     }
  85.  
  86.     init {
  87.         val inflater = LayoutInflater.from(context)
  88.  
  89.         inflater.inflate(R.layout.layout_snackbar, this, true)
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top