Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Adds listener to Android TextView when text has ellipsis.
/**
* Author: Michael Ritchie, ThanksMister LLC
* Date: 10/16/12
* Web: thanksmister.com
*
* Extension of <code>TextView</code> that adds listener for ellipses changes. This can be used to determine
* if a TextView has an ellipses or not.
*
* Derived from discussion on StackOverflow:
*
* http://stackoverflow.com/questions/4005933/how-do-i-tell-if-my-textview-has-been-ellipsized
*/
package com.cg.mobile.components;
import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class EllipsisTextView extends TextView
{
public interface EllipsisListener
{
void ellipsisStateChanged(boolean ellipses);
}
private final List<EllipsisListener> ellipsesListeners = new ArrayList<EllipsisListener>();
private boolean ellipses;
public EllipsisTextView(Context context)
{
super(context);
}
public EllipsisTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public EllipsisTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void addEllipsesListener(EllipsisListener listener)
{
if (listener == null) {
throw new NullPointerException();
}
ellipsesListeners.add(listener);
}
public void removeEllipsesListener(EllipsisListener listener)
{
ellipsesListeners.remove(listener);
}
public boolean hadEllipses() {
return ellipses;
}
@Override
public void layout(int l, int t, int r, int b)
{
super.layout(l, t, r, b);
ellipses = false;
Layout layout = getLayout();
if ( layout != null){
int lines = layout.getLineCount();
if ( lines > 0) {
if ( layout.getEllipsisCount(lines-1) > 0) {
ellipses = true;
}
}
}
for (EllipsisListener listener : ellipsesListeners) {
listener.ellipsisStateChanged(ellipses);
}
}
}

Thank dude

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