Search code examples
androidprogress-barcrouton

Is it possible to use a progress bar in Crouton?


I want to show custom Crouton alert with a progress bar and I want to update the progress dynamicly. This is my code but it doesn't work. How can i do that? Or how can show progress bar while image uploading

private void showCustomViewCrouton() {
    // try {
    View view = getLayoutInflater().inflate(R.layout.crouton_custom_view,null);
    pB = (ProgressBar) view.findViewById(R.id.progressBar1);
    final Crouton crouton;
    final Configuration croutonConfig = new Configuration.Builder()
            .setDuration(Configuration.DURATION_INFINITE)
            .setInAnimation(android.R.anim.fade_in)
            .setOutAnimation(android.R.anim.fade_out).build();
    crouton = Crouton.make(this, view, R.id.croutonContainer, croutonConfig);       
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            crouton.hide(crouton);
        }
    });

    crouton.show();

    try{
        do{
                Thread.sleep(100);
                deger+=1;
                pB.setProgress(deger);          
        }while(deger<101);      
    }catch(Exception e){

    }       
}

Solution

  • Here is how I did it

    final ProgressBar progressBar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal);
    progressBar.setIndeterminate(false);
    
    Crouton ct = Crouton.make(this,progressBar);
    ct.show();
    
    /* Update it While Showing */
    Thread progressUpdateThread = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            for(int i=0;i<100;i++)
            {
                final int progress = i;
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        progressBar.setProgress(progress);
                    }
                });
    
                try
                {
                    Thread.sleep(100);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
    });
    progressUpdateThread.start();