52

How can i show an Activity Indicator in Android? Is there any Android Library given method? If no, please let me know the techniques used to show activity indicator in Android?.

Swati Garg
  • 801
  • 1
  • 9
  • 21
Sanal MS
  • 2,284
  • 4
  • 21
  • 31
  • D'you mean something like standalone infinite progress indicator like one used at ProgressDialog (http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog) – Olegas Feb 17 '11 at 06:21
  • "Activity indicator" is very confusing with the "Activity" class and concept in Android ... did not get the idea that this may be the progress dialog / bar :-) But then there are a few things in Android, where I don't know the right name and am thus not able to google for it well – Heiko Rupp Feb 17 '11 at 06:34

5 Answers5

95

The most direct equivalent of the iOS Activity Indicator in Android is the ProgressBar but set to indeterminate. So you can drop the view into your layout and it will provide you with a spinning animation.

<ProgressBar
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/ctrlActivityIndicator"
    android:indeterminateOnly="true"
    android:keepScreenOn="true"
     />

You could use this to indicate some background activity to the user.

ricardopereira
  • 9,557
  • 4
  • 53
  • 68
Martin Belcher - AtWrk
  • 4,310
  • 3
  • 29
  • 40
  • 2
    how to change the color of the circular bar? – Teja Nandamuri Jun 26 '15 at 17:11
  • To change the colour probably easiest to use Olegas answer then you can specify your own drawable in whatever colour you like. Othewise you might want to copy the SDK source code for the ProgressBar and provide it with your own drawable. – Martin Belcher - AtWrk Jul 01 '15 at 10:34
56

do some thing like this

ProgressDialog mDialog = new ProgressDialog(getApplicationContext());
            mDialog.setMessage("Please wait...");
            mDialog.setCancelable(false);
            mDialog.show();
ingsaurabh
  • 15,042
  • 7
  • 48
  • 77
  • I want to show a activity indicator like in iphone. is it possible to show a busy image? – Sanal MS Feb 17 '11 at 06:34
  • yeah using animation and custom images but that will not let user stop from selecting i.e user might select some button and direct to other activity while you are waiting for an response – ingsaurabh Feb 17 '11 at 06:37
  • 2
    If you end up getting a `WindowManager$BadTokenException` see http://stackoverflow.com/q/1561803/650233 – Dan F May 26 '11 at 18:56
  • 3
    You can use setProgressStyle(STYLE_SPINNER) if you want the dialog to look like it has a spinning icon instead of a progress bar. Equivalent to an iOS ActivityIndicator in an Alert – Martin Belcher - AtWrk Oct 24 '12 at 09:48
  • 6
    That code gave me long error (and crash), part of which was `Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application`. So I used the following instead of the one in answer: `ProgressDialog mDialog = ProgressDialog.show(this, "dialog title", "dialog message", true);` – Sufian Mar 15 '13 at 04:55
  • 3
    If running from an activity you can fix the bad token exception by replacing ProgressDialog mDialog = new ProgressDialog(getApplicationContext()); with ProgressDialog mDialog = new ProgressDialog(this); – Chris Feb 26 '14 at 14:20
  • This code cause a crash on my project, I changed it as new ProgressDialog(getApplicationContext()); It work fine. – tuoxie007 Mar 11 '14 at 02:17
  • 1
    Martin Belcher's answer is the correct one. The docs specifically advises against this usage. See the sidebox in [Android Dialogs](http://developer.android.com/guide/topics/ui/dialogs.html) – Jannie Theunissen May 14 '14 at 12:09
  • JannieT - you're right, it just doesn't work. When for example you rotate the phone sideways, the dialog is "confused" and goes away. – Fattie May 19 '14 at 13:49
  • 2
    ProgressDialog is now marked as obsolete, I don't advise using. – Xander Jan 24 '18 at 15:43
19

There are two other ways of showing activity indicator without using modal ProgressDialog.

You can use ImageView in your layout and apply animation to it. Refer developer's site.

public void startAnimation() {
  // Create an animation
  RotateAnimation rotation = new RotateAnimation(
      0f,
      360f,
      Animation.RELATIVE_TO_SELF,
      0.5f,
      Animation.RELATIVE_TO_SELF,
      0.5f);
  rotation.setDuration(1200);
  rotation.setInterpolator(new LinearInterpolator());
  rotation.setRepeatMode(Animation.RESTART);
  rotation.setRepeatCount(Animation.INFINITE);

  // and apply it to your imageview
  findViewById(R.id.myActivityIndicator).startAnimation(rotation);
}

Or you can use xml-drawable to describe a background image, which will have some rotating animation:

Firstly describe a drawable (in i.e. /res/drawable/my-indicator.xml)

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_black_76"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    android:frameDuration="100" />

Then set it at some view's background

Community
  • 1
  • 1
Olegas
  • 9,749
  • 7
  • 46
  • 70
4
public class Mp3cutterActivity extends Activity {

    MP3Class mp3obj = null;
    TextView tv;
    MP3Class mp3classobj = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context thiscontext = this.getApplicationContext();
        mp3classobj = new MP3Class(thiscontext);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.startbutton);
        tv = (TextView)findViewById(R.id.textview1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //show a wait indicator as well as the function that calls walkSdcard

                try {
                    new SearchSdCard(Mp3cutterActivity.this).execute();
                    // copyProtector.doCopyProtection();
                } catch (Exception e) {
                    System.out.println("in search SD card  " + e.getMessage());
                }
            }

            private void domp3stuff() {
                // TODO Auto-generated method stub
            }
        });
    }
}

class SearchSdCard extends AsyncTask<String, Void, Boolean> {

    Context context;    

    public ProgressDialog dialog;

    public SearchSdCard(Activity activity) {
        this.context = activity;
    }

    protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setMessage("wait for a moment...");
        dialog.show();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        boolean retval = true;
        mp3classobj.searchSdCard();
        return retval;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        if (dialog.isShowing()) {
            dialog.dismiss();
            tv.setText("mp3 cutter is an app which cuts down a chunk of memory \nfrom your sdcard by \ndeleting the .mp3 files and more \nyou were made a BAKRA :-)");
            if(!result){    
                tv.setText("error occured !!");
            }
        }
    }
    //refer below comment for the MP3class.java file details
}
Massimiliano Kraus
  • 3,214
  • 5
  • 20
  • 40
sudatt
  • 57
  • 1
  • 2
  • 1
    public class MP3Class { private Context mcontext; public MP3Class(Context context){ mcontext = context; } public void searchSdCard() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // my function is here u may add yours } } – sudatt Nov 29 '12 at 12:52
1

Activity indicator is nothing but it is known as progress dialog. You can create programmatically.Numbers of tutorials are available, Search how to create progress dialog/ProgressBar

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMax(100);
                progressDialog.setMessage("Its loading....");
                progressDialog.setTitle("ProgressDialog bar example");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
or
progressDialog.setProgressStyle(ProgressDialog.STYLE_CIRCULAR);

or you can create it by using xml , set the visibility visible once task get completed set visibility gone

<ProgressBar
                android:id="@+id/loading_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/grey"
                android:layout_gravity="center" />