2

In my app when i click button the progress bar will be started lately. I want to start start quickly. please can anybody help me. bshowgifts listener in onCreate().

bShowGifts.setOnClickListener(new OnClickListener()  
{                          

@Override        
        public void onClick(View v) 
        {               
            //vsChange
            progressDlg = ProgressDialog.show(Ehome.this, "", "", true);                            
            progressDlg.setContentView(R.layout.custom_dialog);                
            //Gender and age must be selected
            if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)  
            {
                progressDlg.dismiss();  
                String strMsg = "Please select a gender";
                DisplaySelectionMessage(strMsg);             
                return;
            }

            if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
            {
                progressDlg.dismiss();
                String strMsg = "Please select an age";  
                DisplaySelectionMessage(strMsg);               
                return;
            }

            if(nMatchingGifts == 0)
            {
                progressDlg.dismiss(); 
                tvCount = (TextView) findViewById(R.id.textMatchinGifts);
                String strContent = "# of Matching Gifts: 0";
                tvCount.setText(strContent);                                                        
                String strMsg = "No gifts found! Please change the search criteria.";
                DisplaySelectionMessage(strMsg);               
                return; 
            }               

            try
            {                   
                //Thread for getting the negative attributes values
                Thread tDisplayCategories = new Thread() 
                {
                    public void run()
                    {  


                        System.out.println("Showgifts : Thread:run");
                        handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
                    }                                                                                          
                };
                tDisplayCategories.start();
            }
            catch(Exception exp)
            {
                progressDlg.dismiss();
            }
            return;
            }         
    });

    public void DisplaySelectionMessage(String strMsg)
{

            //display alert dialog            
    AlertDialog alertDialog = new AlertDialog.Builder(Ehome.this).create();
    alertDialog.setTitle("Elgifto Alert");
    alertDialog.setMessage(strMsg);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
    {           
        public void onClick(DialogInterface dialog, int id) 
        {                
            dialog.cancel();
        }
    });

    alertDialog.show();
    return;
}

Updated code

bShowGifts.setOnClickListener(new OnClickListener()  

{                          
        @Override        
            public void onClick(View v) 
        {               
          new showgiftsbtn().execute();
            }         
});

public class showgiftsbtn extends AsyncTask<Void, Void, Void>

{

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

        protected void onPreExecute()
        {
            this.dialog.setMessage("Please Wait...");
            this.dialog.show();



        // put your code which preload with processDialog  
        if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)  
        {
            dialog.dismiss();  
            String strMsg = "Please select a gender";
            DisplaySelectionMessage(strMsg);             
            return;
        }

        if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
        {
            dialog.dismiss();
            String strMsg = "Please select an age";  
            DisplaySelectionMessage(strMsg);               
            return;
        }

        if(nMatchingGifts == 0)
        {
            dialog.dismiss(); 
            tvCount = (TextView) findViewById(R.id.textMatchinGifts);
            String strContent = "# of Matching Gifts: 0";
            tvCount.setText(strContent);                                                        
            String strMsg = "No gifts found! Please change the search criteria.";
            DisplaySelectionMessage(strMsg);               
            return; 
        }               

        try
        {                   
            //Thread for getting the negative attributes values
            Thread tDisplayCategories = new Thread() 
            {
                public void run()
                {  
                    System.out.println("Showgifts : Thread:run");
                    handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
                    dialog.dismiss();
                }                                                                                          
            };
            tDisplayCategories.start();
        }
        catch(Exception exp)
        {
            dialog.dismiss();
        }
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {           
        // put you code here
        return null;
    }
    protected void onPostExecute(final Void unused)
    {           

    }
}
thirtydot
  • 224,678
  • 48
  • 389
  • 349
naresh
  • 10,332
  • 25
  • 81
  • 124

2 Answers2

4

try this:: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute

    private class xyz extends AsyncTask<Void, Void, Void> {
                private final ProgressDialog dialog = new ProgressDialog(tranning.this);

                protected void onPreExecute() {
                    this.dialog.setMessage("Please Wait...");
                    this.dialog.show();

                    // put your code which preload with processDialog  


            @Override
            protected Void doInBackground(Void... arg0) {

                // put you code here
if(ElGiftoAppData.getAppData().arSelectedGender.size() == 0)  
            {

                String strMsg = "Please select a gender";
                DisplaySelectionMessage(strMsg);             
                return;
            }

            if(ElGiftoAppData.getAppData().arSelectedAge.size() == 0)
            {
                progressDlg.dismiss();
                String strMsg = "Please select an age";  
                DisplaySelectionMessage(strMsg);               
                return;
            }

        if(nMatchingGifts == 0)
        {

            tvCount = (TextView) findViewById(R.id.textMatchinGifts);
            String strContent = "# of Matching Gifts: 0";
            tvCount.setText(strContent);                                                        
            String strMsg = "No gifts found! Please change the search criteria.";
            DisplaySelectionMessage(strMsg);               
            return; 
        }               

        try
        {                   
            //Thread for getting the negative attributes values
            Thread tDisplayCategories = new Thread() 
            {
                public void run()
                {  


                    System.out.println("Showgifts : Thread:run");
                    handlerDisplayGifts.post(call_ShowGiftsCategoryPage);
                }                                                                                          
            };
            tDisplayCategories.start();
        }
        catch(Exception exp)
        {

        }
            }

                return null;
            }
protected void onPostExecute(final Void unused) {
                if (this.dialog.isShowing()) {
                  this.dialog.dismiss();

                }

            }
        }

and use this in your button click event ::

 new xyz().execute();
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • click event means click listener. is it correct? i put like this bShowGifts.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new showgiftsbtn().execute(); } }); – naresh Sep 08 '11 at 07:44
  • public void onClick(View v) { new xyz().execute(); – Nikunj Patel Sep 08 '11 at 07:45
  • AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute – Nikunj Patel Sep 08 '11 at 07:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3265/discussion-between-naresh-and-nik) – naresh Sep 08 '11 at 07:53
  • its not possible to chat over here – Nikunj Patel Sep 08 '11 at 07:54
  • this will working at background and increase your app performance – Nikunj Patel Sep 08 '11 at 10:32
0

Use Handler to show ProgressBar() ....

private Handler mHandler = new Handler();

final Runnable mUpdateResults = new Runnable() {
       public void run() {
      progressDlg = ProgressDialog.show(Ehome.this, "", "", true);                            
            progressDlg.setContentView(R.layout.custom_dialog);
       }
};

put the above code above your class code and call this where you show progressBar(); like this:

mHandler.post(mUpdateResults );
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63