简体   繁体   中英

Making progress bar in Android

I wish to do something like a progress bar animation in Android app.

I'm trying to make a simple line which Width increases.

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
for(int i = 0; i < 100; i++) {
    ImageView iv = (ImageView) findViewById(R.id.init_line);
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(i, 2);
    iv.setLayoutParams(parms);
    try { Thread.sleep(20); }
    catch(Exception e) { }
}

However is not working and it doesn't appear nothing on the screen. It would be great if you guys could give me a hand on this. Thank you very much

Android has a Predefined class for a progress bar, its in a class called ProgressDialog. It looks like this:

在此处输入图片说明

You can add this codes in your onCreate() and set the state of the progressDialog like this:

ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);

For more details you can check the Android API Documentation here

This site has a full application demo about it, you might want to check it out.

You might want to put your code in onResume or a method that is called when the Activity (or Fragment) is visible to the user. onCreate is called when the Activity is first created and is where you should do your static setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM