-1

I loading some data from a database and I'd like to display a circle animation during this, because it's take time. The circle appears but only after loadContent() which load data.

The code:

final String PREFS_NAME = "MyPrefsFile";

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    ProgressBar p = (ProgressBar) findViewById(R.id.imgProgress);
    p.setEnabled(true);


    if (settings.getBoolean("my_first_time", true)) {

        // the app is being launched for first time, do something
        Log.d("Launched:", "first time");

        loadContent();

        // record the fact that the app has been started at least once
        settings.edit().putBoolean("my_first_time", false).commit();
    }
    p.setEnabled(false);

In XML:

<ProgressBar
    android:id="@+id/imgProgress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content" />
user3399567
  • 67
  • 1
  • 6

2 Answers2

0

I don't know what is loadContent method but I'd suggest you to do an AsyncTask (if it's not the case). You will be able to show/hide your ProgressBar as follows:

// loadContent method
public class loadContent extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        // set your progress bar to visible
        ((ProgressBar) findViewById(R.id.imgProgress)).setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... params) {
        // do some stuff: load datas from sql, load images.. whatever
        // and return a string value for onPostExecute (string = "Loaded"
        return string;
    }

    @Override
    protected void onPostExecute(String result) {
        if(result.equals("Loaded") {
            // the loading is finish, display your datas
            // set your progress bar to gone
            ((ProgressBar) findViewById(R.id.imgProgress)).setVisibility(View.GONE);
        }
    }
}  

Here and here, you will see a good example of an AsyncTask method. Then, you just have to add the visibility attribute to your view as:

<ProgressBar
    android:id="@+id/imgProgress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:visibility="gone" />  

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
0

Why not just do this ?

public class SomeTask extends AsyncTask<Void, String, Void>{

        ProgressDialog dialogue;

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            dialogue.dismiss();

            //If Using some adapter; someadapteradapter.notifyDataSetChanged();
        }

        @Override
        protected void onPreExecute() {
            dialogue = new ProgressDialog(ProgressBar.this);
            dialogue.setTitle("Loading items..");
            dialogue.show();
            super.onPreExecute();

        }

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

            //Your task!!

            return null;


        }
    }

What can be more simple than this..:)

mike20132013
  • 5,357
  • 3
  • 31
  • 41