안드로이드에서 ProgressBar 를 이용하는 예제

progress.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/linearlayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="onBtnStart"

        android:text="시작" />

    <ProgressBar

        android:id="@+id/progressBar1"

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

</LinearLayout>


package test.android.hello;


import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.ProgressBar;


public class ProgressBarActivity extends Activity {


ProgressBar progress;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.progressbar);

progress = (ProgressBar)findViewById(R.id.progressBar1);

progress.setMax(100);

progress.setProgress(0);

}


public void onBtnStart(View v){

Log.i("버튼", "ProgressBar 시작됨");

new Thread(){

public void run() {

for(int i=0;i<=100;i++) {

progress.setProgress(i);

progress.postInvalidate();

try {

Thread.sleep(200);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();

}

}


Posted by cwisky