1. Download our Official Android App: Forums for Android!

Apps ProgressBar problem?

Discussion in 'Android Development' started by Grendizer, Jun 22, 2011.

  1. Grendizer

    Grendizer Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    10
    Joined:
    Mar 14, 2010

    Mar 14, 2010
    10
    0
    15
    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:
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical" android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent">
    6.  
    7.  
    8.     <WebView xmlns:android="http://schemas.android.com/apk/res/android"
    9.         android:id="@+id/webview" android:layout_width="fill_parent"
    10.         android:layout_height="fill_parent" />
    11.  
    12.     <ProgressBar android:layout_width="wrap_content"
    13.         android:layout_height="wrap_content" android:id="@+id/progressbar_default" />
    14.        
    15.     <ProgressBar android:layout_width="fill_parent"
    16.         android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"
    17.         android:id="@+id/progressbar_Horizontal" android:max="100" />
    18.  
    19. </LinearLayout>
    PHP:
    1. import java.io.*;
    2. import java.net.*;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.os.Environment;
    7. import android.os.Handler;
    8. import android.os.Message;
    9. import android.view.KeyEvent;
    10. import android.webkit.DownloadListener;
    11. import android.webkit.WebView;
    12. import android.webkit.WebViewClient;
    13. import android.widget.ProgressBar;
    14.  
    15. public class Download extends Activity {
    16.  
    17.    WebView mWebView;
    18.    ProgressBar myProgressBar;
    19.    int myProgress = 0;
    20.  
    21.    @Override
    22.    public void onCreate(Bundle savedInstanceState) {
    23.       super.onCreate(savedInstanceState);
    24.       setContentView(R.layout.download);
    25.  
    26.       myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
    27.       new Thread(myThread).start();
    28.      
    29.       // WebView
    30.       mWebView = (WebView) findViewById(R.id.webview);
    31.       mWebView.getSettings().setJavaScriptEnabled(true);
    32.       mWebView.loadUrl("http://www.dt031g.programvaruteknik.nu/dialpad/sounds/");
    33.       mWebView.setWebViewClient(new DownloadWebViewClient());
    34.       mWebView.setDownloadListener(new DownloadListener() {
    35.  
    36.          @Override
    37.          public void onDownloadStart(String url, String userAgent,
    38.                String contentDisposition, String mimetype,
    39.                long contentLength) {
    40.             // download from the remote server
    41.             try {
    42.  
    43.                URL u = new URL(url);
    44.                HttpURLConnection urlConnection = (HttpURLConnection) u
    45.                      .openConnection();
    46.                urlConnection.setRequestMethod("GET");
    47.                urlConnection.setDoOutput(true);
    48.                // and connect!
    49.                urlConnection.connect();
    50.  
    51.                File SDCardRoot = Environment.getExternalStorageDirectory();
    52.                File file = new File(SDCardRoot, "temp.zip");
    53.                FileOutputStream fileOutput = new FileOutputStream(file);
    54.                InputStream inputStream = urlConnection.getInputStream();
    55.  
    56.                byte[] buffer = new byte[1024];
    57.                int bufferLength = 0;
    58.                
    59.                while ((bufferLength = inputStream.read(buffer)) > 0) {
    60.                   // add the data in the buffer to the file in the file
    61.                   // output stream (the file on the sd card
    62.                   fileOutput.write(buffer, 0, bufferLength);
    63.                }
    64.                // close the output stream when done
    65.                fileOutput.close();
    66.  
    67.                String fileName = Environment.getExternalStorageDirectory()
    68.                      + "/temp.zip";
    69.                String dest = Environment.getExternalStorageDirectory()
    70.                      + "/dialpad/sounds/";
    71.                ZIP.decompress(fileName, dest);
    72.  
    73.                file.delete();
    74.             } catch (MalformedURLException e) {
    75.                e.printStackTrace();
    76.             } catch (ProtocolException e) {
    77.                e.printStackTrace();
    78.             } catch (FileNotFoundException e) {
    79.                e.printStackTrace();
    80.             } catch (IOException e) {
    81.                e.printStackTrace();
    82.             }
    83.          }
    84.       });
    85.    }
    86.  
    87.    private Runnable myThread = new Runnable() {
    88.  
    89.       @Override
    90.       public void run() {
    91.          while (myProgress < 100) {
    92.             try {
    93.                myHandle.sendMessage(myHandle.obtainMessage());
    94.                Thread.sleep(1000);
    95.             } catch (Throwable t) {
    96.             }
    97.          }
    98.       }
    99.  
    100.       Handler myHandle = new Handler() {
    101.  
    102.          @Override
    103.          public void handleMessage(Message msg) {
    104.             myProgress++;
    105.             myProgressBar.setProgress(myProgress);
    106.          }
    107.       };
    108.    };
    109.  
    110.    @Override
    111.    public boolean onKeyDown(int keyCode, KeyEvent event) {
    112.       if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    113.          mWebView.goBack();
    114.          return true;
    115.       }
    116.       return super.onKeyDown(keyCode, event);
    117.    }
    118.  
    119.    private class DownloadWebViewClient extends WebViewClient {
    120.       @Override
    121.       public boolean shouldOverrideUrlLoading(WebView view, String url) {
    122.          view.loadUrl(url);
    123.          return true;
    124.       }
    125.    }
    126. }
    127.  
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    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.
     

Share This Page

Loading...