투케이2K

455. (kotlin/코틀린) [Material] 머터리얼 디자인 사용해 Custom Snackbar 커스텀 스낵바 생성 본문

Kotlin

455. (kotlin/코틀린) [Material] 머터리얼 디자인 사용해 Custom Snackbar 커스텀 스낵바 생성

투케이2K 2024. 1. 18. 20:25
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Kotlin

 

[소스 코드]

        // -----------------------------------------------------------------------------------------
        // TODO [SEARCH FAST] : [Snackbar] : [BLACK : 커스텀 스낵바 메시지 출력]
        // -----------------------------------------------------------------------------------------
        // TODO [필요 의존성 및 style 설정]
        // -----------------------------------------------------------------------------------------
        /*
        implementation 'com.google.android.material:material:1.8.0' // TODO [컴파일, 빌드 타겟 33 인 경우]

        <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        </style>
        */
        // -----------------------------------------------------------------------------------------
        // TODO [호출 방법]
        // -----------------------------------------------------------------------------------------
        /*
        C_Ui_View.showSnackBarBlack(this@A_Intro, window.decorView, "알림", "내용", "확인")
        */
        // -----------------------------------------------------------------------------------------
        fun showSnackBarBlack(mContext: Context, view: View, title: String, message: String, button: String) {
            // ===============================================================
            S_Log._F_(mContext, "BLACK : 커스텀 스낵바 메시지 출력", arrayOf(
                "title :: $title",
                "message$message"
            ))
            // ===============================================================
            try {
                Handler(Looper.getMainLooper()).postDelayed({
                    if (mContext != null) {

                        // TODO [스낵바 표시 데이터]
                        var data = ""
                        if (C_Util.stringNotNull(title) === true) {
                            data = "[$title]"
                        }
                        if (C_Util.stringNotNull(message) === true) {

                            // [이미 data 에 저장된 데이터가 있는 경우]
                            data += if (C_Util.stringNotNull(data) === true) {
                                " : $message"
                            } else {
                                message
                            }
                        }


                        // TODO [스낵바 표시 수행]
                        val contentLayout = view.findViewById<View>(android.R.id.content) // [안드로이드 content View 지정]

                        //Snackbar.make(contentLayout, String.valueOf(data), Snackbar.LENGTH_LONG) // [레이아웃 / 메시지 / 표시 시간]
                        Snackbar.make(contentLayout, data, Snackbar.LENGTH_INDEFINITE) // [레이아웃 / 메시지 / 표시 지속]
                            .setBackgroundTint(Color.BLACK) // [배경 색상]
                            .setTextColor(Color.WHITE) // [텍스트 색상]
                            .setActionTextColor(Color.YELLOW) // [버튼 색상 변경]
                            //.setDuration(10000) // [10 초 표시 유지]
                            .setTextMaxLines(20) // [텍스트 줄 표시 라인]
                            .setAction(button) { // [버튼 클릭 이벤트 정의]

                            }.show()
                    }
                }, 0)

            } catch (e: Exception) {
                S_Log._printStackTrace_(mContext, S_FinalMsg.LOG_BUG_STATE, null, e)
            }
        }
 

[결과 출력]


반응형
Comments