Wednesday, January 30, 2013

Dalam tutorial ini penulis akan berbagi tentang cara membuat progress bar atau bahasa sehari-harinya loading program gitu..... hehe. Ok langsung saja ke TKP
1. Buat project


2. Kemudian isikan nama project yang akan dibuat, seperti pada gambar dibawah ini : 

3. Klik Next 

4. Klik Next
5. Pada bagian activity_main.xml, seperti pada gambar berikut :

 ketik kode berikut :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btnStartProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File" />

</LinearLayout>

6. Pada bagian MainActivity.java, seperti pada gambar berikut :

ketik kode berikut :
package com.progresbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButton();

}

public void addListenerOnButton() {

btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {

// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();

//reset progress bar status
progressBarStatus = 0;

//reset filesize
fileSize = 0;

new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {

// process some tasks
progressBarStatus = doSomeTasks();

// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}

// ok, file is downloaded,
if (progressBarStatus >= 100) {

// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();

}

});

}

// file download simulator
public int doSomeTasks() {

while (fileSize <= 1000000) {

fileSize++;

if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
// ...add your own

}

return 100;

}

}
7. Setelah itu jalankan program dengan tombol Ctrl + F11
8. Hasil dari program....

Sekian tutorial singkat ini semoga bermanfaat....

1 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete