Help us understand the problem. What is going on with this article?

Toast (with custom XML Layout) with variable display time

More than 1 year has passed since last update.

The native Android Toast has only two options for message duration (SHORT or LONG). Here we define a custom Toast, which uses a custom XML layout, and whose display duration is calculated to be proportional to the length of the Toast message (measured in number of characters).

  • the custom XML Layout is inflated with a inflater service, and assigned to the basic toast "view" member
  • a basic toast is displayed with the SHORT duration, but is actually shown multiple times in a countdown loop until the predefined display time has ended
class CustomToast {

    companion object {
        // NOTE: we must create a new custom toast for each tick
        public fun show(context: Context?, text: String)  {
            context?.runOnUiThread {
                val durationinMillisecondsPerCharacter: Long = 50
                val duration: Long = text.length * durationinMillisecondsPerCharacter
                val timer = object : CountDownTimer(duration, 1000) {
                    override fun onFinish() {
                        setCustomLayoutView(context, text)?.show()
                    }
                    override fun onTick(p0: Long) {
                        setCustomLayoutView(context, text)?.show()
                    }
                }
                timer.start()
            }
        }

        private fun setCustomLayoutView(context: Context, text: String) : Toast? {
            val toast = Toast(context)
            toast.setDuration(Toast.LENGTH_SHORT)
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view = inflater.inflate(R.layout.layout_custom_toast, null)
            view.findViewById<TextView>(R.id.custom_toast_text)?.let{
                it.setText(text)
            }
            toast.view = view
            return toast
        }
    }
}
cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした