0

I have a text in a TextView and need to highlight each sentence for 3 seconds. I tried Thraed timer and handler.postDelayed. But did not wotk in the loop.

How can I implement delay in a FOR loop?

My code :

        Button btnPlay = (Button) findViewById(R.id.btnPlay);
    btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            TextView txtMain = (TextView) findViewById(R.id.txtMain);
            String textString = txtMain.getText().toString();
            ArrayList<Integer> Sentences = new ArrayList<Integer>();

            // Save indexes of sentences in array
            Sentences.add(0);
            for (int i = 0; i < textString.length(); i++) {
                if (textString.charAt(i) == '.') {
                    Sentences.add(i);
                }
            }

            // Need 3 seconds delay at the end of this loop
            for (int j = 0; j < Sentences.size() - 1; j++) {
                Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
                spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), Sentences.get(j), Sentences.get(j + 1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtMain.setText(spanText);
            }
        }

    });

Thanks in advance

teocka
  • 127
  • 1
  • 1
  • 8

0 Answers0