• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ProgressBar problem?

Grendizer

Newbie
Hi all

I'm having problem displaying my progressbar. It's suppose to show up when I download a file inside a WebView.
I'd appreciate any help here.

XML
PHP:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ProgressBar android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/progressbar_default" />
        
    <ProgressBar android:layout_width="fill_parent"
        android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/progressbar_Horizontal" android:max="100" />

</LinearLayout>
PHP:
import java.io.*;
import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class Download extends Activity {

   WebView mWebView;
   ProgressBar myProgressBar;
   int myProgress = 0;

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

      myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
      new Thread(myThread).start();
      
      // WebView
      mWebView = (WebView) findViewById(R.id.webview);
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.loadUrl("http://www.dt031g.programvaruteknik.nu/dialpad/sounds/");
      mWebView.setWebViewClient(new DownloadWebViewClient());
      mWebView.setDownloadListener(new DownloadListener() {

         @Override
         public void onDownloadStart(String url, String userAgent,
               String contentDisposition, String mimetype,
               long contentLength) {
            // download from the remote server
            try {

               URL u = new URL(url);
               HttpURLConnection urlConnection = (HttpURLConnection) u
                     .openConnection();
               urlConnection.setRequestMethod("GET");
               urlConnection.setDoOutput(true);
               // and connect!
               urlConnection.connect();

               File SDCardRoot = Environment.getExternalStorageDirectory();
               File file = new File(SDCardRoot, "temp.zip");
               FileOutputStream fileOutput = new FileOutputStream(file);
               InputStream inputStream = urlConnection.getInputStream();

               byte[] buffer = new byte[1024];
               int bufferLength = 0;
               
               while ((bufferLength = inputStream.read(buffer)) > 0) {
                  // add the data in the buffer to the file in the file
                  // output stream (the file on the sd card
                  fileOutput.write(buffer, 0, bufferLength);
               }
               // close the output stream when done
               fileOutput.close();

               String fileName = Environment.getExternalStorageDirectory()
                     + "/temp.zip";
               String dest = Environment.getExternalStorageDirectory()
                     + "/dialpad/sounds/";
               ZIP.decompress(fileName, dest);

               file.delete();
            } catch (MalformedURLException e) {
               e.printStackTrace();
            } catch (ProtocolException e) {
               e.printStackTrace();
            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      });
   }

   private Runnable myThread = new Runnable() {

      @Override
      public void run() {
         while (myProgress < 100) {
            try {
               myHandle.sendMessage(myHandle.obtainMessage());
               Thread.sleep(1000);
            } catch (Throwable t) {
            }
         }
      }

      Handler myHandle = new Handler() {

         @Override
         public void handleMessage(Message msg) {
            myProgress++;
            myProgressBar.setProgress(myProgress);
         }
      };
   };

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
         mWebView.goBack();
         return true;
      }
      return super.onKeyDown(keyCode, event);
   }

   private class DownloadWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }
}
 
The first issue I see at a glance is that your webview is filling the parent linear layout, so you will never see the progressbar because the webview will have filled the screen.

I'd say start with working on your layout and see how things go from there.
 
Back
Top Bottom