Task
Task
Task
Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
ProgressBar pb;
Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb=(ProgressBar) findViewById(R.id.progressBar1);
startBtn=(Button) findViewById(R.id.startBtn);
//ONCLICK
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new Downloader().execute();
}
});
}
class Downloader extends AsyncTask<Void, Integer, Integer>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//SET PB PROIPERTIES
pb.setMax(100);
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
//UPDATE PROGRESSBAR
pb.setProgress(values[0]);
@Override
protected Integer doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
.xml
<RelativeLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
tools_context=".MainActivity" >
<ProgressBar
android_id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android_layout_width="match_parent"
android_layout_height="50dp"
android_layout_alignParentTop="true"
android_layout_centerHorizontal="true"
android_layout_marginTop="198dp" />
<Button
android_id="@+id/startBtn"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_below="@+id/progressBar1"
android_background="#009968"
android_layout_centerHorizontal="true"
android_layout_marginTop="69dp"
android_text="Start" />
</RelativeLayout>