Indeterminate Loading Text in Android

Filed Under (android, java, programming) by Nathan Schwermann on 01-08-2010

Recently I made a handy little Android widget that displays a message and animates 3 periods after it to indicate that work is being done. Its just another alternative to using a ProgressBar set to indeterminate. I used the timed UI updating technique described in the devguide

First we extend the TextView class and included some extra members.

 public class LoadingTextWidget extends TextView {

        Handler mHandler;
        StringBuilder mBuilder;
        String mText;
        int mCount, mStringSize;
        final int DELAY = 400;

        public LoadingTextWidget(Context context, AttributeSet attrs) {
 	super(context, attrs);
		mText = (String)getText();
		mBuilder = new StringBuilder(mText);
		mCount = 0;
		mHandler = new Handler();
		mStringSize = (int) getPaint().measureText(mText + "...");
		setWidth(mStringSize);
		mHandler.post(AnimatePeriod);
	}

Next we implement a runnable to animate the periods every 400 ms.

	Runnable AnimatePeriod = new Runnable() {

		@Override
		public void run() {
			mCount++;
			mBuilder.append('.');
			if(mCount == 4){
				mCount = 0;
				mBuilder.delete(0, mBuilder.length());
				mBuilder.append(mText);
			}
			setText(mBuilder.toString());
			setWidth(mStringSize);
			mHandler.postDelayed(AnimatePeriod, DELAY);
		}
	};

Lastly remember to clean up after yourself.

	@Override
	protected void onDetachedFromWindow() {
		mHandler.removeCallbacks(AnimatePeriod);
		super.onDetachedFromWindow();
	}
}

Now we just create the object in an Android layout file.




  


One thing to note about the style attributes is that the gravity is set to left. This way the actual text doesn’t move on the screen as the periods animate.