开发者

Android Activity Indicator?

How can i show an Activity Indicator in Android? Is th开发者_开发技巧ere any Android Library given method? If no, please let me know the techniques used to show activity indicator in Android?.


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.


do some thing like this

ProgressDialog mDialog = new ProgressDialog(getApplicationContext());
            mDialog.setMessage("Please wait...");
            mDialog.setCancelable(false);
            mDialog.show();


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


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
}


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜