4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ProgressDialogが非推奨のため、ProgressBarで代替する

Posted at

ProgressDialog が API 26 (Android 8.0) から非推奨となったため、代わりに ProgressBar などを用いて実装を行う必要があります。

Referenceには ProgressDialog クラスがありますが、DialogFragment クラスを継承しただけの独自クラスです。

シンプルに実装できるものがありましたので紹介します。

以下、Kotlinのサンプルコードです。

MainActivity.kt
class MainActivity : AppCompatActivity() {

    // ProgressDialog クラスはこのあと実装します
    private val progressDialog = ProgressDialog.newInstance("表示するメッセージ")
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ここでProgressDialogを表示させます
        progressDialog.show(supportFragmentManager,"TAG")
    }
}
ProgressDialog.kt
class ProgressDialog : DialogFragment() {
 
    companion object {
        fun newInstance(message: String): ProgressDialog {
            val instance = ProgressDialog()
            val arguments = Bundle()
            arguments.putString("message", message)
            instance.arguments = arguments
            return instance
        }
    }
 
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val mMessage = arguments!!.getString("message")
 
        val builder = AlertDialog.Builder(activity!!)
        val inflater = activity!!.layoutInflater
        val view = inflater.inflate(R.layout.dialog_progress, null)
        val mMessageTextView = view.findViewById(R.id.progress_message) as TextView
        mMessageTextView.text = mMessage
        builder.setView(view)
        return builder.create()
    }
}
dialog_progress.xml
<!-- ルートタグを変更しています。 -->
<!-- エラーが出ましたので、AndroidX を使うようにしました。 -->

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp" />

    <TextView
        android:id="@+id/progress_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@id/progress"
        app:layout_constraintLeft_toRightOf="@id/progress"
        app:layout_constraintTop_toTopOf="@id/progress"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

Reference

ProgressDialogの独自実装
https://wiki.toridge.com/index.php?android-kotlin-DialogFragment

4
10
0

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
4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?