1

I am developing an android app where I need to show a ProgressBar until the loop execution is finished. After execution it will go to the nextActivity.

here is my java code

    public class Image_Recognition extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image__recognition);

        Button btnNextPage = (Button) findViewById(R.id.btnNextPage);


            btnNextPage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

                        Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                        SparseArray<TextBlock> items = textRecognizer.detect(frame);
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < items.size(); ++i) {
                            TextBlock item = items.valueAt(i);
                            stringBuilder.append(item.getValue());
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append('.');
                        txtResult.setText(stringBuilder.toString());


                        str = stringBuilder.toString();
                        Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
                        myintent.putExtra("translate", str);
                        startActivity(myintent);

                }
            });
        }
    //}
}
tahsin_siad
  • 86
  • 1
  • 11

3 Answers3

0

You have two problems here.

ProgressBar, which you can use ProgressDialog or a ProgressBar in your XML and show it, while hiding the rest of your IU.

Blocking the UI

You are running your loop directly from you click listener, which means that you will block your UI and even if you show some sort of progress, it will be blocked till your loop is over, you need to start a thread and run your loop in.

Here's a not very clean version of you click listener impl

final ProgressDialog dialog = new ProgressDialog(Image_Recognition.this);
        dialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<TextBlock> items = textRecognizer.detect(frame);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < items.size(); ++i) {
                    TextBlock item = items.valueAt(i);
                    stringBuilder.append(item.getValue());
                    stringBuilder.append("\n");
                }
                stringBuilder.append('.');
                txtResult.setText(stringBuilder.toString());

                str = stringBuilder.toString();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
                        myintent.putExtra("translate", str);
                        startActivity(myintent);
                    }
                });
            }
        }).start();
elmorabea
  • 3,243
  • 1
  • 14
  • 20
0

You could use Async Task and run your loop in doInBackground()

class MyTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;
    public MyTask(ProgressDialog pd){
        this.pd=pd;
    }
    @Override
    protected void onPreExecute () {
        super.onPreExecute();
        pd.setMessage("Uploading . . .");
        pd.show();
        pd.setCancelable(false);
    }

    @Override
    protected Void doInBackground (Void...voids){
        for(int i=0;i<10000000;i++){
            System.out.print(i);
        }
        return null;//download file
    }

    @Override
    protected void onPostExecute (Void aVoid){
        super.onPostExecute(aVoid);
        pd.dismiss();
    }
}

and in activity call new MyTask(pd).execute();

Harpreet Singh
  • 543
  • 2
  • 10
  • 29
0

You can try with this.

final ProgressDialog dialog = new ProgressDialog(Image_Recognition.this);
btnNextPage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {



                    dialog.show(Image_Recognition.this, "", "Please wait...", true);



                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

                            if (!textRecognizer.isOperational())
                                Log.e("ERROR", "Detector dependencies are not yet available");
                            else {

                                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                                SparseArray<TextBlock> items = textRecognizer.detect(frame);
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < items.size(); ++i) {
                                    TextBlock item = items.valueAt(i);
                                    stringBuilder.append(item.getValue());
                                    stringBuilder.append("\n");
                                }
                                stringBuilder.append('.');
                                txtResult.setText(stringBuilder.toString());

                                str = stringBuilder.toString();
                            }

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.dismiss();
                                    dialog.setCancelable(true);
                                    Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
                                    myintent.putExtra("translate", str);
                                    startActivity(myintent);
                                }
                            });
                        }
                    },5000);
Md Khairul Islam
  • 597
  • 5
  • 14