Androidプログラマへの道 〜 Moonlight 明日香 〜 - プログレスバーを作成する
プログレスバーは, 主に実行中の処理の進行状況を表示するためのものである.

水平プログレスバーを表示する


  • ProbBar01.java
    • findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したプログレスバーのインスタンスを取得する.
    • ProgressBar#setMaxメソッドで, 最大値を設定する.
    • ProgressBar#setProgressメソッドで, プログレスバーの値を設定する.
    • ProgressBar#setSecondaryProgressメソッドで, プログレスバーのセカンダリ値を設定する.
package com.moonlight_aska.android.progressbar01;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;

public class ProgressBar01 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ProgressBar progBar = (ProgressBar)findViewById(R.id.progbar_id);
    // 最大値を設定する.
    progBar.setMax(100);
    // プログレスバーの値を設定する.
    progBar.setProgress(50);
    // セカンダリ値を設定する.
    progBar.setSecondaryProgress(70);
  }
}
  • res/layout/main.xml
    • styleで水平プログレスバーを指定する.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <ProgressBar android:id="@+id/progbar_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    style="?android:attr/progressBarStyleHorizontal"
    />
</LinearLayout>

円プログレスバーを表示する



  • res/layout/main.xml
    • styleで円プログレスバーの大きさを設定する.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <ProgressBar android:id="@+id/progbar01_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    style="?android:attr/progressBarStyleSmall"
    />
  <ProgressBar android:id="@+id/progbar02_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    style="?android:attr/progressBarStyle"
    />
  <ProgressBar android:id="@+id/progbar03_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    style="?android:attr/progressBarStyleLarge"
    />
</LinearLayout>