LoginSignup
1

More than 3 years have passed since last update.

DialogFragmentでProgress Dialogを作った

Last updated at Posted at 2019-05-29

デモ

progress.gif

ソースコード

ライブラリ化はしていないので、使う場合はコピペしてください。

使い方

MainActivity.kt
class MainActivity : AppCompatActivity() {

    companion object {
        const val TAG_PROGRESS_DIALOG = "progress"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button_show_dialog.setOnClickListener {
            showProgressDialog()
        }
    }

    private fun showProgressDialog() {
        val dialog = ProgressDialogFragment.newInstance()
        dialog.show(supportFragmentManager, TAG_PROGRESS_DIALOG)
        GlobalScope.launch(Dispatchers.Main) {
            dialog.setMessage("Running (10%)")
            dialog.setProgress(10)
            delay(1000)
            dialog.setMessage("Running (50%)")
            dialog.setProgress(50)
            delay(1000)
            dialog.setMessage("Running (100%)")
            dialog.setProgress(100)
            delay(1000)
            dialog.dismiss()
        }
    }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1