Search code examples
javaandroiddelayhandler

Handler does not delay - Android Studio Java


I am working on a project. My program shows a video by videoview and get values from an excel file. I am having difficulty in showing the values one by one. I am trying to use some delay methods. I have used Thread.sleep and SystemClock.sleep methods but they freeze the entire app and phone. Now I am trying to use handler method. I don't receive any error but there is no delay. I am putting my code below.

If anyone can help me with this delay, I will be very happy.

package com.example.exceldeneme;

import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.MediaController;
import android.widget.VideoView;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.String;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.List;


public class MainActivity<number_cars> extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView videoView = findViewById(R.id.video_view);
        Button  button = findViewById(R.id.button);
        TextView text_cars = findViewById(R.id.textEmpty);
        TextView text_total_lots = findViewById(R.id.textTotalLots);
        readData();
        int number_of_park = 100;
        text_total_lots.setText(String.valueOf(number_of_park));
        int counter = 0;

        String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.camera_out;
        Uri uri = Uri.parse(videoPath);
        videoView.setVideoURI(uri);
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);
        
        for (String i : number_of_cars.get(0)){
            counter = counter + 1 ;
        }

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int counter = 0;
                for (String i : number_of_cars.get(0)){
                    counter = counter + 1 ;
                }
                for (int i = 1; i < counter-1;i= i +1){
                    String count_park = number_of_cars.get(0)[i];
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            text_cars.setText(count_park);
                        }
                    }, 1000);


                    //SystemClock.sleep(1000); //ms
                    Log.d("Selam","SORUN YOK");
                }
            }
        });


    }


    private ArrayList<String[]> number_of_cars = new ArrayList<String[]>();
    private void readData() {
        InputStream is = getResources().openRawResource(R.raw.numberofcars);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is, Charset.forName("UTF-8"))
        );
        String line = "";
            try {
                while(((line = reader.readLine()) != null)) {
                String[] tokens = line.split(",");
                                number_of_cars.add(tokens);
                Log.d("MyActivity","Just created"+ Arrays.toString(tokens));
                }
            }
            catch (IOException e) {
                Log.wtf("MyActivity","Error reading data file on line" + line, e);
                e.printStackTrace();
            }
    }
}

Solution

  • for the isolated issue you probably want :

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            text_cars.setText(count_park);
                        }
                    }, 1000 * i);
    

    But you should really have a single instance Handler and post all Runnable objects to it, with a way to remove all pending Runnable, if any when ui is destroyed using : Handler::removeCallbacksAnMessages(null) in the relevant hook method like onDestroy.