custom Snackbar

nagosooo·2022년 6월 22일
0

andriod

목록 보기
25/27

snackBar의 gravity, marin, background변경, drawable 삽입, animation 지정 등을 해보자!

//customSnackbar.kt

class SampleSnackBar(view: View, private val message: String, private val drawable: Drawable) {

    companion object {
        fun make(view: View, message: String, drawable: Drawable) =
            SampleSnackBar(view, message, drawable)
    }

    private val context = view.context
    private val snackBar = Snackbar.make(view, "", 2000)
    private val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout

    private val inflater = LayoutInflater.from(context)
    private val snackBarBinding: CustomBinding =
        DataBindingUtil.inflate(inflater, R.layout.custom, null, false)

    init {
        initView()
        initData()
    }

    private fun initView() {
        with(snackBarLayout) {
            val layoutParams = layoutParams as FrameLayout.LayoutParams
            
            //애니메이션 추가 
            //정석은 snackBar을 extend한 class 에서 contentViewCallback 을 커스텀 해주는거 같음
            //그러나 나는 야매로 snackbarLayout에 animation을 줬다. 
            val snackBarShowAnim = AnimationUtils.loadAnimation(context, R.anim.snackbar_show)
            val snackBarHideAnim = AnimationUtils.loadAnimation(context, R.anim.snackbar_hide)
            this.startAnimation(snackBarShowAnim) // 👈 시작할때 애니메이션 

            Handler(Looper.getMainLooper()).postDelayed({
                this.startAnimation(snackBarHideAnim)
            }, 2000L) // 👈 핸들러로 메인 쓰레드 2초 잠재우고, 스낵바 hide 하는 애니메이션 실행 
            
            layoutParams.gravity = Gravity.TOP // 👈 gravity 설정
            removeAllViews()
            setPadding(0, 16, 0, 0) // 👈 padding 설정. 위에서 16만큼 떨어져있게. 
            setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent))
            addView(snackBarBinding.root, 0)
        }

    }

    private fun initData() {
        snackBarBinding.tvSample.text = message
        snackBarBinding.image.background = drawable
    }

    fun show() {
        snackBar.show()
    }
}
//custom.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:cardBackgroundColor="#2B353E"
            app:cardCornerRadius="20dp"
            app:cardElevation="0dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_gravity="center_horizontal"
                android:paddingStart="16dp"
                android:paddingEnd="16dp">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:src="@drawable/check_1" />

                <TextView
                    android:id="@+id/tvSample"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginEnd="20dp"
                    android:text="Hello World!"
                    android:textColor="@android:color/white"
                    android:textSize="15dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toEndOf="@id/image"
                    app:layout_constraintTop_toTopOf="parent" />


            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>
    </androidx.appcompat.widget.LinearLayoutCompat>
</layout>
//style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Common">
        <attr name="android:src"/> // 👈 스낵바 이미지를 커스텀 할 수 있도록 style 에 지정 
    </declare-styleable>
</resources>
//snackbar show anim.xml
//위에서 아래로 내려오는 fade in 효과 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <translate
        android:duration="400"
        android:fromYDelta="-100%"
        android:toYDelta="0%" />
    <alpha
        android:duration="400"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
</set>
//snackbar hide anim.xml
//아래에서 위로 올라가는 fade out 효과 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <translate
        android:duration="400"
        android:fromYDelta="0%"
        android:toYDelta="-100%" />
    <alpha
        android:duration="400"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />
</set>
//실제사용 

val drawable = ContextCompat.getDrawable(this, R.drawable.check_1)!!
SampleSnackBar.make(binding.root, "월요일 좋아", drawable).show()

참고 블로그 - https://black-jin0427.tistory.com/294

0개의 댓글