Created
October 16, 2012 23:10
-
-
Save thanksmister/3902660 to your computer and use it in GitHub Desktop.
Adds listener to Android TextView when text has ellipsis.
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
/** | |
* 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working in recyclerview