Skip to content

Instantly share code, notes, and snippets.

@MikeFot
Last active July 15, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeFot/e52e42625e8b4e583a4d78333198c6ac to your computer and use it in GitHub Desktop.
Save MikeFot/e52e42625e8b4e583a4d78333198c6ac to your computer and use it in GitHub Desktop.
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Written by Michael Fotiadis
* https://gist.github.com/MikeFot
*/
public class DotsTextView extends TextView {
private static final int INTERVAL = 350;
private static final String[] VALUES = {" ", ". ", ".. ", "..."};
private int mCurrentPosition;
private String mPrefix = "";
private Handler mHandler;
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
updateStatus();
mHandler.postDelayed(mStatusChecker, INTERVAL);
}
};
public DotsTextView(Context context) {
super(context);
}
public DotsTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DotsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DotsTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
this.mCurrentPosition = 0;
this.mHandler = new Handler(Looper.getMainLooper());
}
public void startRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
mStatusChecker.run();
}
public void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
public void setPrefix(String mPrefix) {
this.mPrefix = mPrefix;
}
private void updateStatus() {
if (mCurrentPosition == VALUES.length) {
mCurrentPosition = 0;
}
setText(String.format("%s%s", mPrefix, VALUES[mCurrentPosition]));
mCurrentPosition++;
}
}
@MikeFot
Copy link
Author

MikeFot commented Jun 9, 2016

Simple text view that animates 3 trailing dots after a loading message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment