LoginSignup
0
0

More than 3 years have passed since last update.

ProgressBarで待機中を表示

Last updated at Posted at 2020-10-02

はじめに

何らかのWEB APIをリクエストして処理待ち。
そういことよくありますよね。
そういった待機中なのを示すのに、ProgressBarを使ってみました。

ProgressBar

ProgressBarはいくつかのstyleがあり、またコードを書かなくてもレイアウトで設定するだけで動きます。

  • Widget.ProgressBar.Horizontal
  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Inverse
  • Widget.ProgressBar.Small.Inverse
  • Widget.ProgressBar.Large.Inverse

これらのstyleがデフォルトで用意されています。
今回は Widget.ProgressBar.Large を使いました。

デモ

簡単なサンプルを作成しました。

progress_sample.gif

コード

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

        val progressBar = findViewById<ProgressBar>(R.id.progress_bar)
        val button = findViewById<Button>(R.id.button)

        button.setOnClickListener({
            if (progressBar.visibility == ProgressBar.VISIBLE) {
                progressBar.visibility = ProgressBar.INVISIBLE
                button.text = getString(R.string.start)
            } else {
                progressBar.visibility = ProgressBar.VISIBLE
                button.text = getString(R.string.stop)
            }
        })
    }
}
activity_main.xml
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="@android:style/Widget.ProgressBar.Large"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="@string/stop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progress_bar" />

</androidx.constraintlayout.widget.ConstraintLayout>

振り返り

用意されたstyleを使って、円か棒で進捗を表示するようです。
もっとカスタマイズしたいという要望がなければ、これで十分な気がします。
追加でやるなら、不透明な背景を挿入するとか、サークルの色を変更するとかですかね。
styleを独自で用意すれば、もっとリッチにできるのでしょうか?

他の人がどうやっているのか知りたいので、俺はこうやっているとか、もう少し見た目がリッチなライブラリがあるとか、コメントください!

0
0
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
0
0