-1

I'm simply loading an image on button click via url , i want progress bar to show downloadin done,0-100 now i had changed the code not showing the progress updation but only progress bar kindly help. XML has a button [downloadbtn], and a progressbar, imageview and a quit button

mainfest.xml has acsess permission internet

code:

*
package com.example.t4;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ProgressBar prgs;
    ProgressTask task = new ProgressTask();
    Button showProgressBtn;
    Button stopProgressBtn;
    ImageView img;
    Bitmap bmp ;
    TextView tv1;
    int filesize,filedyn;

    public void startdownload(){

        try { URL url;
        url = new URL("http://whywedoit.files.wordpress.com/2009/04/smile.jpg");
        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();

        filesize = urlConnection.getContentLength();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1=(TextView)findViewById(R.id.tv1);
        img = (ImageView) findViewById(R.id.imageView1) ;
        stopProgressBtn = (Button) findViewById(R.id.btnCancel);
        prgs  = (ProgressBar) findViewById(R.id.progress);
        showProgressBtn = (Button) findViewById(R.id.btn);
        prgs.setVisibility(View.INVISIBLE);
        img.setVisibility(View.INVISIBLE);
        tv1.setVisibility(View.INVISIBLE);

        final ProgressTask pgt = new ProgressTask();


        showProgressBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if(pgt.getStatus()==Status.RUNNING){Toast.makeText(getApplicationContext(), "Status"+pgt.getStatus(), Toast.LENGTH_SHORT).show();}
                if(pgt.getStatus()==Status.FINISHED||pgt.getStatus()==Status.PENDING){
                Toast.makeText(getApplicationContext(), "Status"+pgt.getStatus(), Toast.LENGTH_SHORT).show();
                prgs.setVisibility(View.VISIBLE);
                task.execute(10);
                tv1.setVisibility(View.VISIBLE);
            }}
        });

        stopProgressBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stopProgress();

            }
        });

    }

    private class ProgressTask extends AsyncTask<Integer,Integer,Void>{

        protected void onPreExecute() {
            super.onPreExecute(); 
            prgs.setMax(100); // set maximum progress to 100.

        }
        protected void onCancelled() {
            prgs.setMax(0); // stop the progress
            Log.v("Progress","Cancelled");
            finish();
        }
        protected Void doInBackground(Integer... params) {
            startdownload();
          //for(int j=0;j<=filesize;j++){
            publishProgress((int) ((filedyn / (float) filesize) * 100));
            //}
            Log.v("Progress","Downloading");

            return null;
        }
        protected void onProgressUpdate(Integer... values) {


            prgs.setProgress(0);

            Log.v("Progress","Updating");
        }
        protected void onPostExecute(Void result) {
            img.setVisibility(View.VISIBLE);
            tv1.setVisibility(View.INVISIBLE);
            img.setImageBitmap(bmp);
            prgs.setVisibility(View.INVISIBLE);
            Log.v("Progress", "Finished");

        }

    }



    public void stopProgress() {
        task.cancel(true);  
    }

}


*
Jayant
  • 35
  • 1
  • 9

3 Answers3

0

use this code and use progressdialog instead of progressbar

  @Override
        protected void onProgressUpdate(Integer... progress) {
            //Set the pertange done in the progress dialog
            pd.setProgress((int) (progress[0]));
        }
Anand Phadke
  • 531
  • 3
  • 28
0

You need set the ProgressBar max value and you need to update the progress in onProgressUpdate

protected void onProgressUpdate(Integer... values) {
     prgs.setProgress(values[0]);
     Log.v("Progress","Once");
}

and call

publishProgress(progress) 

in the doInBackground of ProgressTask to update the progress

Check for more ref

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

Sorry, I don't think you only have progress bar update problems. There are too many points needs to be repaired.

1st inside your OnClickListener, you are doing task.execute(10);showProgress(); AsnycTask is asynchronous task, you cannot put your showProgress() there. Do something with your code.

private boolean init = true;
private Void doInBackground(Integer... params) {
    init = true;
    startdownload();
    Log.v("Progress", "Downloading");
    return null;
}
protected void onProgressUpdate(Integer... values) {
    if (init){
        showProgress();
        init = false;
    }else{
        prgs.setProgress(5);
        Log.v("Progress", "Once");
    }   
}

2nd startdownload method, are you sure your startdownload method doing downloading? or just getting the length of filesize. Make it clear else you will get yourself confuse.

3nd To invoke onProgressUpdate, you need to call publishProgress(progressValue); Here's the sample.

private Void doInBackground(Integer... params) {
    init = true;
    startdownload();
    publishProgress(5);   // onProgressUpdate invoked

    //
    // doing download
    //

    Log.v("Progress", "Downloading");
    return null;
}
edisonthk
  • 1,403
  • 16
  • 35