0

This is my first class doing Url loading .

public class HelloWebViewClient extends WebViewClient
{    
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)
     {
         view.loadUrl(url);
         return true;
     }
}

This is my Activity Class where data is displacing in web view.

public class detailedview extends Activity
{
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailedview);
        GetSet gs = new GetSet();

        String title = gs.getTitle();
        String desc = gs.getDesc();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setTextSize(TextSize.SMALLER);
        mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
        mWebView.setWebViewClient(new HelloWebViewClient());
    }
}

I want to put progress bar for loading data in my activity, I have tried a lot but I can't do it.

Can you please write code or post code so that I can have a progress bar while loading data according to my code.

huon
  • 94,605
  • 21
  • 231
  • 225
user1388681
  • 73
  • 3
  • 7

3 Answers3

1

add this code to your code

ProgressDialog pd = new ProgressDialog(detailedview .this);
                                       ^^^^^^^^^^^^
        pd.setMessage("Loading...");
        pd.show(); <-----

mWebView.setWebViewClient(new WebViewClient() {
^^^^^^^^    

@Override 
public void onPageFinished(WebView view, String url) { <-----
     super.onPageFinished(view, url);
     pd.dismiss(); <-----
   }
});
-------------------------------------------------------------------------------


public class detailedview extends Activity
{
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailedview);
        ProgressDialog pd = new ProgressDialog(detailedview .this);

        pd.setMessage("Loading...");
        pd.show(); 

       mWebView.setWebViewClient(new WebViewClient() {


@Override 
public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url);
     pd.dismiss(); 
   }
});
        GetSet gs = new GetSet();

        String title = gs.getTitle();
        String desc = gs.getDesc();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setTextSize(TextSize.SMALLER);
        mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
      //  mWebView.setWebViewClient(new HelloWebViewClient());
    }
}
MAC
  • 15,799
  • 8
  • 54
  • 95
0

You can use below code

public class Grab extends Activity {

WebView view;
private ProgressDialog progressDialog = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.grab);

    view = (WebView) findViewById(R.id.WebView01);
    view.setBackgroundColor(0);  // set the background transparent 

    progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER    );

    view.setWebChromeClient(new MyWebChromeClient());
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    view.loadUrl("http://www.wepware.com/web/apps/jots.do");
}


private class MyWebViewClient extends WebViewClient {
    @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)      //override WebViewClient method to open url in activity
    {
        view.loadUrl(url);
        return true;
    }
}

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if(progressDialog!=null){
            if ( newProgress >=80 ) {
                progressDialog.dismiss();
            } else {
                progressDialog.setMessage(newProgress + " % loaded");
            }
        }
        super.onProgressChanged(view, newProgress);
    }
}
}
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • but in which class we ll write this code in (HelloWebViewClient ) or (detailedview ) – user1388681 Jun 18 '12 at 09:10
  • as you copied code from here it will show to remove Override. So you can remove it. And it will work – Nirali Jun 18 '12 at 09:18
  • HelloWebViewClient, detailedview these are two class please post me complete code so that we can show progress bar for loading data ??? – user1388681 Jun 18 '12 at 09:30
  • You can see my code and apply it in your class.. I have put my whole activity code here. – Nirali Jun 18 '12 at 09:33
0

first make one class say it DownloadWebPageTask

put this in ur oncreate method

GetSet gs = new GetSet();

    String title = gs.getTitle();
    String desc = gs.getDesc();

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setTextSize(TextSize.SMALLER);

now make an object of that class like this

DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(); Note:**task.execute(); will call ur class "DownloadWebPageTask" u can start this from any line of code write now i have put this in onCreate Method

and here is ur class

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

     private final ProgressDialog dialog = new ProgressDialog(Home.this);

      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Loading...");
         this.dialog.show();
      }
    @Override
    protected String doInBackground(String... urls) {
        String response = "";

      mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
         if (this.dialog.isShowing()) {
                this.dialog.dismiss();
           }

             mWebView.setWebViewClient(new HelloWebViewClient());
    }


}   

Note:**Whenever u use any progress bar than in on post excute method all background process are done till than ur progress bar will be displyed and as soon as data downloading is completed than onPostExcute method will be call.and data downloaded from external server will be stored in webview or u can do any thing with that. If u still find any query than tell me Best Of luck Aamirkhan I.

Aamirkhan
  • 5,746
  • 10
  • 47
  • 74