Moments of growth

[Kotlin] Custom Toast ์ค‘๋ณต ๋ฐฉ์ง€ํ•˜๊ธฐ ๋ณธ๋ฌธ

Android [Kotlin] ๐Ÿ’ป๐Ÿค

[Kotlin] Custom Toast ์ค‘๋ณต ๋ฐฉ์ง€ํ•˜๊ธฐ

๋ฎค๋ง์ด 2022. 6. 12. 16:08

์ปค์Šคํ…€ ํ† ์ŠคํŠธ๋ฅผ ์“ธ ๋•Œ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ† ์ŠคํŠธ๊ฐ€ ํ•˜๋‚˜์”ฉ ์‹คํ–‰๋˜๋ฉด ์ฆ‰๊ฐ์ ์œผ๋กœ ๋ณ€ํ•˜๋ฉด์„œ ๋ณด์—ฌ์ค˜์•ผํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋Š” ์ ์ ˆํ•˜์ง€ ๋ชปํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ํ† ์ŠคํŠธ๋Š” duration์„ LENGTH.SHORT์™€ LENGTH.LONG ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋˜๊ณ  ์ปค์Šคํ…€ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

object CustomToast {
    fun createToast(context: Context, message: String): Toast? {
        val inflater = LayoutInflater.from(context)
        val binding: ToastCustomBinding =
            DataBindingUtil.inflate(inflater, R.layout.toast_custom, null, false)

        binding.tvSample.text = message
        
        return Toast(context).apply{
            setGravitysetGravity(Gravity.CENTER, 0, 0)
            duration = Toast.LENGTH_SHORT
            view = binding.root
        }
}

์œ„์˜ ์›๋ž˜ ์ฝ”๋“œ์—์„œ ์•„๋ž˜๋กœ ๋ฐ”๊ฟ”์คฌ๋‹ค.

object CustomToast {
    var toast: Toast? = null
    fun createToast(context: Context, message: String): Toast? {
        val inflater = LayoutInflater.from(context)
        val binding: ToastCustomBinding =
            DataBindingUtil.inflate(inflater, R.layout.toast_custom, null, false)

        binding.tvSample.text = message

        if (toast == null) {
            toast = Toast(context)
        }

        toast?.setGravity(Gravity.CENTER, 0, 0)
        toast?.duration = Toast.LENGTH_SHORT
        toast?.view = binding.root

        return toast
    }
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ˆŒ๋ €์„ ๋•Œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ํ† ์ŠคํŠธ๋งŒ ๋ณด์—ฌ์ง€๊ฒŒ ๋œ๋‹ค.

 

toast ์˜ ๊ฐ’์ด null ์ผ ๊ฒฝ์šฐ Toast(context) ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ๊ณ  null์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” Toast ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์กฐ??? -> ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๊ธดํ•จ

 

 

์ถœ์ฒ˜)

 

Android Toast ์ค‘๋ณต๋ฐฉ์ง€ | ๋ฉ”๋ชจ๊ด‘

๋ฉ”๋ชจ๊ด‘ > ํ”„๋กœ๊ทธ๋ž˜๋ฐ > Android > Android Toast ์ค‘๋ณต๋ฐฉ์ง€ Android Toast ์ค‘๋ณต๋ฐฉ์ง€ adsloader 2020๋…„ 8์›” 10์ผ

vintageappmaker.com

 

 

 

์•„๋ž˜์˜ ์‚ฌ์ดํŠธ๋“ค์€ ๋ชจ๋‘ ๊ธฐ๋ณธ toast ์ค‘๋ณต ๋ฐฉ์ง€์ด๋‹ค.

 

[์•ˆ๋“œ๋กœ์ด๋“œ] Toast ๊ธฐ์กด ๋ฉ”์‹œ์ง€ ์‚ญ์ œ, ํ˜„์žฌ ๋ฉ”์‹œ์ง€๋งŒ ๋ณด์ด๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•

Android์—์„œ Toast๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๋ฉ”์‹œ์ง€๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋œจ๋ฉด์„œ ์˜ค๋žซ๋™์•ˆ ์ง€์†๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ๋ฉ”์‹œ์ง€๋Š” ์‚ญ์ œํ•˜๊ณ , ํ˜„์žฌ ๋ฉ”์‹œ์ง€๋งŒ ๋ณด์ด๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. 1.    Toast ๊ฐ์ฒด ์„ ์–ธ pu

rockdrumy.tistory.com

 

[Android] Toast ํ•˜๋‚˜๋งŒ ๋„์šฐ๊ธฐ (Toast ์ทจ์†Œํ•˜๊ธฐ)

ํ† ์ŠคํŠธ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ„๋‹จํ•œ ์•Œ๋ฆผ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ํ•˜์ง€๋งŒ ํ† ์ŠคํŠธ๊ฐ€ ์ค‘๋ณต์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒฝ์šฐ ์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๊ณ„์†ํ•ด์„œ ํ† ์ŠคํŠธ๊ฐ€ ๋‚˜์™€ ์‚ฌ์šฉ์ž๊ฐ€ ์ฆ‰๊ฐ์ ์ธ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์ง€ ๋ชปํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์„ฑ

latte-is-horse.tistory.com

 

Comments