4

I have a asynchtask for some database operation

Before I start the task I call this function, to show a prograssBar The ProgressBar is in a RelativLayout because someone on the net says that calling setVisibility(); on the progressBar doesn´t work in some chases (and it doesn´t for me either)

 public static void showProgressBar()
{
    Log.e("TEST","Try to show ProgressBar, Visibiliy: " + prog_bar.getVisibility());
    if(prog_bar != null)
    {
        prog_bar.setVisibility(RelativeLayout.VISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

on PostExecute i call the function:

 public static void hideProgressBar()
{
    Log.e("TEST","Try to hide ProgressBar, Visibiliy: " + prog_bar.getVisibility());
    if(prog_bar != null)
    {
        prog_bar.setVisibility(RelativeLayout.INVISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

Strange thing is that the methods are called correct, also the Logs are correct and no error appears, but no progressBar shows up.

Android 2.2, Windows 7, Tested on Emulator and GalaxyTab

Best regards schwandi

EDIT:

Changed my methods to:

  public static void hideProgressBar()
{

    if(prog_bar != null)
    {
        Log.e("TEST","Try to hide ProgressBar, Visibiliy: " + prog_bar.getVisibility());
        prog_bar.setVisibility(View.GONE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

public static void showProgressBar()
{

    if(prog_bar != null)
    {
        Log.e("TEST","Try to show ProgressBar, Visibiliy: " + prog_bar.getVisibility());
        prog_bar.setVisibility(View.VISIBLE);
        Log.e("TEST","Success");
    }
    else
    {
        Log.e("TEST","prog_bar != null");
    }
    Log.e("TEST","Visibiliy: " + prog_bar.getVisibility());
}

still doens´t work.

Also i changed that prog_bar is the ProgressBar itself.

  prog_bar = (ProgressBar) findViewById(R.id.progressBar1);
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
schw4ndi
  • 231
  • 4
  • 18

1 Answers1

0

Try putting your whole block of code initiating the asyncTask into a worker thread like this (leave out the onClick() if that's not relevant):

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {

            // your entire asyncTask initiation code block here

        }
    }).start();
}

Here is how it solved my own problem & here are the docs.

Community
  • 1
  • 1
rockhammer
  • 957
  • 2
  • 13
  • 38