Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Task

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import android.app.

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

//DO HEAVY JOB


for(int i=0;i<100;i++)
{
publishProgress(i);
try
{
Thread.sleep(100);
}catch(InterruptedException ie)
{
ie.printStackTrace();
}
}

return null;
}

@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

Toast.makeText(getApplicationContext(), "Download Finished !!",


Toast.LENGTH_LONG).show();
}

.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>

You might also like