You need to set the default visibility progressBar gone.and onPreExecute() set Visible and onPostExecute() set gone.
<ProgressBar style="?android:attr/progressBarStyle" android:layout_centerInParent="true" android:id="@+id/progressbar_loading" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@+id/listViewGameResults" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="1dip" android:layout_below="@+id/upperstrip" android:layout_above="@+id/ivDownStrip" />
Your activity should look like this
public class demo extends Activity{ private ListView listViewGameResults; protected View dialogLayout; protected ArrayList<Game> listGames; progressBar progress; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.adresults); progress=(ProgressBar)findViewByid(R.id.progressbar_loading); GameResultsLoader gameResultsLoader = new GameResultsLoader(this); gameResultsLoader.execute(); } }
Use one separate class for AsyncTask
public class GameResultsLoader extends AsyncTask<Void, Void, Void> { private GameResultsAdapter adapter; Demo demo; public GameResultsLoader(Demo demo) { this.demo=demo; } @Override protected void onPreExecute() { demo.progress.setvisibility(View.Visible); } @Override protected Void doInBackground(Void... params) { try { listGames = GameResultsCache.getInstance().getGameResults(); adapter = new GameResultsAdapter(getBaseContext(), listGames); listViewGameResults = (ListView)findViewById(R.id.listViewGameResults); } catch (Exception e) {
Dilip source share