Android-progressbar
ProgressDialogを使用したAndroidプログレスバー
進行状況バーは、タスクの進行状況を示すために使用されます。 たとえば、インターネットから何かをアップロードまたはダウンロードするときは、ダウンロード/アップロードの進行状況をユーザーに表示することをお勧めします。
Androidには、プログレスバーを作成できるProgressDialogというクラスがあります。 これを行うには、このクラスのオブジェクトをインスタンス化する必要があります。 その構文は次のとおりです。
ProgressDialog progress = new ProgressDialog(this);
これで、このダイアログのいくつかのプロパティを設定できます。 スタイル、テキストなど。
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
これらのメソッドとは別に、ProgressDialogクラスによって提供される他のメソッドがあります
Sr. No | Title & description |
---|---|
1 |
getMax() このメソッドは、進行状況の最大値を返します。 |
2 |
incrementProgressBy(int diff) このメソッドは、パラメーターとして渡された値の差だけ進行状況バーを増分します。 |
3 |
setIndeterminate(boolean indeterminate) このメソッドは、進行状況インジケータを確定または不確定に設定します。 |
4 |
setMax(int max) このメソッドは、進捗ダイアログの最大値を設定します。 |
5 |
setProgress(int value) このメソッドは、特定の値で進行状況ダイアログを更新するために使用されます。 |
6 |
show(Context context, CharSequence title, CharSequence message) これは、進行状況ダイアログを表示するために使用される静的メソッドです。 |
例
この例は、実際にはプログレスバーであるプログレスダイアログの水平方向の使用を示しています。 ボタンを押すと進行状況バーが表示されます。
この例を試すには、以下の手順に従ってアプリケーションを開発した後、実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add progress code to display the progress dialog. |
3 | Modify res/layout/activity_main.xml file to add respective XML code. |
4 | Run the application and choose a running android device and install the application on it and verify the results. |
以下は、変更されたメインアクティビティファイル* src/MainActivity.java。*の内容です。
package com.example.sairamkrishna.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button2);
}
public void download(View view){
progress=new ProgressDialog(this);
progress.setMessage("Downloading Music");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
*res/layout/activity_main.xml* の内容を次のように変更します-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Progress bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="download"
android:id="@+id/button2"
android:layout_marginLeft="125dp"
android:layout_marginStart="125dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
これはデフォルトの AndroidManifest.xml です-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスがコンピューターに接続されていることを前提としています。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します-
Android Progress Dialog Tutorial
ボタンを押すだけでプログレスバーが起動します。 押すと、次の画面が表示されます-
Android Progress Dialog Tutorial
継続的に更新されます。