Get a solution using Spannable
The following is the Thread class, which runs continuously at intervals of 100 ms.
class MyThread extends Thread { //used for stopping thread boolean flag; //init flag to true so that method run continuously public MyThread() { flag = true; } //set flag false, if want to stop this thread public void setFlag(boolean flag) { this.flag = flag; } @Override public void run() { super.run(); while (flag) { try { Thread.sleep(intervalMiliSeconds); runOnUiThread(new Runnable() { @Override public void run() { Spannable spn = new SpannableString(txtView .getText().toString()); spn.setSpan(new ForegroundColorSpan(Color.WHITE), startPosition, endPosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); txtView.setText(spn); startPosition++; endPosition++; endPosition %= (lengthOfString + charGaps); startPosition %= lengthOfString; if (startPosition == 0) { endPosition = charGaps; startPosition = 0; } if (endPosition > lengthOfString) { endPosition = lengthOfString; } Log.d("Home", "Start : " + startPosition + " End : " + endPosition); } }); } catch (Exception e) { e.printStackTrace(); } } } }
To use the above code, use the following implementation.
TextView txtView = (TextView) findViewById(R.id.txtView); int charGaps = 3; int startPosition = 0; int endPosition = charGaps; int lengthOfString = txtView.getText().length(); MyThread thread = new MyThread(); thread.start();
Output

Even better answers will be more valuable. :)
source share