Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill progress bar in a specific amount of time?

Tags:

android

I have the following splash screen for my app:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src = "@drawable/timer_ic"
        android:layout_centerInParent="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Timer"
        android:textColor="#FFFFFF"
        android:id="@+id/textView"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="51dp" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="250sp"
        android:layout_height="5sp"
        android:progress="1"
        android:layout_marginTop="82dp"
        android:max="3"
        android:indeterminate="false"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:progressDrawable="@drawable/custom_progress_bar"
        android:background="#808080"/>
</RelativeLayout>

The splash screen runs for 3 seconds before moving onto the next activity, which is controlled by this piece of code:

package com.awesomeapps.misael.timer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ProgressBar;


public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openMainActivity= new Intent("com.awesomeapps.timer.MAINACTIVITY");
                    startActivity(openMainActivity);
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

Like I said and as you can see the splash screeb runs for 3 seconds (3000 milliseconds). You may also notice that the splash screen has a progress bar.

My question is:

How can I fill/load the progress bad in those 3 seconds that my splash screen is running?

I guess to give my app that sort of loading effect when it first starts by filling the progress bar.

Is it possible to do this?

like image 968
AmateurProgrammer Avatar asked Jul 01 '16 06:07

AmateurProgrammer


People also ask

How do you change how much of the progress bar is filled in?

You need to call setLevel on the ClipDrawable. It takes a value from 0 to 10000. So, progress. setLevel(2500) would be 25% full.

How do I customize my progress bar?

Customizing a ProgressBar requires defining the attribute or properties for the background and progress of your progress bar. You can do this in the XML file or in the Activity (at run time). @hirengamit res is "Resource". Your error is probably indicating that the file cannot be found.


3 Answers

I would get rid of yout timer Thread and instead use ValueAnimator to animate progress bar with duration of 3 seconds

ValueAnimator animator = ValueAnimator.ofInt(0, progressBar.getMax());
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation){
        progressBar.setProgress((Integer)animation.getAnimatedValue());
    }
});
animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        // start your activity here
    }
});
animator.start();
like image 184
Hellboy Avatar answered Sep 19 '22 12:09

Hellboy


you can use a countdown timer for that instead of Thread or Handler

 private CountDownTimer countDownTimer =
        new CountDownTimer(10000, 100) {
            public void onTick(long millisUntilFinished) {
                progressBar.setProgress(Math.abs((int) millisUntilFinished / 100 - 100));

                }
            }

            public void onFinish() {
               // you can do your action
            }

        };

to start count down timer

  countDownTimer.start();
like image 41
Vishnu V S Avatar answered Sep 18 '22 12:09

Vishnu V S


    new Thread(new Runnable() {
        public void run() {
            doWork();
            startApp();
            finish();
        }
    }).start();
}

private void doWork() {
    for (int progress=1; progress<=100; progress+=1) {
        try {
            Thread.sleep(50);  // 5 seconds
            load.setProgress(progress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private void startApp() {
    Intent intent = new Intent(Splash.this, MainActivity.class);
    startActivity(intent);
}
like image 26
user15793478 Avatar answered Sep 19 '22 12:09

user15793478