Last active
July 15, 2016 13:44
-
-
Save MikeFot/e52e42625e8b4e583a4d78333198c6ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple text view that animates 3 trailing dots after a loading message.