Increase ProgressBar through ANDROID loop

0

I have a splash screen which has a progressbar where in the future I will perform the bd existence checks of the application etc. But now I would like to understand how to make that bar progress for example through a for loop.

How would the code that should add to the current so that instead of waiting 3000ms to move from the splash screen to the main activity. You have to fill in the progressbar at that time by means of a loop

JAVA:

public class SplashScreenActivity extends Activity {

// Set the duration of the splash screen
private static final long SPLASH_SCREEN_DELAY = 3000;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set portrait orientation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.splash_screen);

    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            // Start the next activity
            Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, MainActivity.class);
            startActivity(mainIntent);

            // Close the activity so the user won't able to go back this
            // activity pressing Back button
            finish();
        }
    };

    // Simulate a long loading process on application startup.
    Timer timer = new Timer();
    timer.schedule(task, SPLASH_SCREEN_DELAY);
}

}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:id="@+id/splash_screenImView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/app_name"
    android:scaleType="fitXY"
    android:src="@drawable/splash_screen" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true">

    <ProgressBar
        android:id="@+id/splash_screenProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginBottom="?attr/actionBarSize"
        android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>
    
asked by Eduardo 22.04.2017 в 03:01
source

1 answer

0

Define a MAX_VALUE parameter for your ProgressBar and save the reference of ProgressBar in a global variable:

public ProgressBar splash_screenProgressBar;
public int MAX_VALUE = 30; // Aqui puedes poner el valor que desees

Now look for the reference in the layout and initialize your ProgressBar:

splash_screenProgressBar = (ProgressBar) findViewById(R.id.splash_screenProgressBar);
splash_screenProgressBar.setMax(MAX_VALUE);

Now create a Timer any, personally I like more this:

new CountDownTimer(3000, 100) {

       int progreso = 1; // Variable que va a ir aumentando del progreso
       @Override
       public void onTick(long millisUntilFinished) {
            splash_screenProgressBar.setProgress(progreso);
            progreso += (1);
       }

       @Override
       public void onFinish() {
           progress.setProgress(MAX_VALUE);
       }
   }.start();

If you notice, the CountDownTimer receives two parámetros . The first is the duration and the second is every few more is going to happen so that it enters onTic k. It means that the value at which progress will be increasing must be proportional to the time defined in order for it to enter the onTick and increase its value.

In this example I used a value of 30 for the MAX_VALUE , since OnTick will be called 30 times in 3 seconds and I am increasing it with a value of 1 each time. Because it will enter every 0.10ms (100) as defined in the timer parameters. When finished, in case the progress has not been completed due to perhaps a bad calculation or inaccuracy, since it only accepts whole values the .setProgress() of the progressBar, I define the Progress as MAX_VALUE to complete the progress of the bar. You can handle it with your logic as you wish.

Greetings.

    
answered by 22.04.2017 / 04:00
source