본문 바로가기
프로그래밍/Kotlin

[Kotlin] ProgressBar 코드로 구현

by Youngs_ 2022. 12. 15.

1번

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable

class LoadingDialog
    constructor(context: Context) : Dialog(context){

    init {
        setCanceledOnTouchOutside(false)

        window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        setContentView(R.layout.dialog_loading)
    }
}

혹은 아래코드를 사용한다. 필자는 아래코드만 써봤지만 위 코드가 더 간결한듯?

위 코드는 XML파일을 하나 만들어야 하지만 아래 코드는 XML 코드를 따로 만들지 않아도 사용할수있다.


2번

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.view.*
import android.widget.LinearLayout
import android.widget.ProgressBar

object NetworkProgressDialog {

    private var dialog: Dialog? = null

    fun start(contextParam: Context) {
        val context = contextParam
        val unit = if (dialog == null || dialog?.context !== context) {
            dialog?.dismiss()
            dialog = null

            val linearLayout = LinearLayout(context)
            linearLayout.gravity = Gravity.CENTER
            linearLayout.setBackgroundColor(Color.TRANSPARENT)


            val progressBar = ProgressBar(context)
//            progressBar.indeterminateDrawable.setColorFilter(
//                ContextCompat.getColor(
//                    context,
//                    android.R.color.holo_red_light
//                ), PorterDuff.Mode.SRC_IN
//            ) // 색 지정, deprecate됨
            linearLayout.addView(
                progressBar,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            )

            dialog = Dialog(context)
            dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
            if (dialog?.window != null) {
                dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
            }
            dialog?.setContentView(
                linearLayout,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            dialog?.setCancelable(false)
            dialog?.setOnKeyListener { dialog, keyCode, event -> keyCode == KeyEvent.KEYCODE_SEARCH && event.repeatCount == 0 }

            dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // Dialog 뒷 배경 검정색으로 나오지않게하기

            dialog?.show()
        } else if (dialog?.isShowing == false) {
            dialog?.show()
        }
        else
            dialog?.dismiss()
    }

    fun end() {
        if (dialog != null && dialog?.isShowing == true) {
            dialog?.dismiss()
            dialog = null
        }
    }
}

 


1번 코드 출처 : https://programmar.tistory.com/20

 

안드로이드 Kotlin으로 Custom Loading Dialog(커스텀 로딩 다이얼로그) 구현하기

안녕하세요! 허접샴푸입니다. 정말 오랜만에 돌아왔습니다 ㅜㅜ 앞으로 Kotlin으로 많은 Tip을 제공해드리도록 하겠습니다. 먼저 Kotlin으로 Custom Dialog 을 구현하는 방법을 알려드리겠습니다. 많은

programmar.tistory.com

댓글