Skip to content Skip to sidebar Skip to footer

Android Progressbar

hi could some one show me how to add a progress bar to this method : public boolean sendFile(String path,String ip, int port) { // TODO Auto-generated method stub

Solution 1:

ProgressDialog pbarDialog =  new ProgressDialog( mContext ); 
pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pbarDialog.setMessage("Loading...");
pbarDialog.setCancelable(false);
pbarDialog.setMax(100);
 pbarDialog .setProgress(0);
 pbarDialog.show();
    while ((bytesRead = fileIn.read(buffer)) > 0) {
                    out.write(buffer, 0, buffer.length);
      //get the previous value of progress bar int old_value = pbarDialog .getProgress();
                    //calculate how much did you read from the fileint new_read =(int)( ((float)  bytesRead/f.length()) )*100 ) ;
                    //add the new read to the old_valueintvalue = new_read+old_value;
                    pbarDialog.setProgress(value);              
      System.out.println("SO sendFile" + bytesRead +filename);
                }
 pbarDialog.dismiss();

Solution 2:

You would need to run that function from the UI thread in order to show a progress bar, but then you wouldn't get to see any progress until the function finished. The right approach for what you are trying to do is an AsyncTask, check

http://developer.android.com/reference/android/os/AsyncTask.html

Post a Comment for "Android Progressbar"