Utilizzando textView.getLayout() getEllipsisStart (0) funziona solo se Android: SingleLine = "true"
Ecco una soluzione che funziona se Android: maxLines è impostato:
public static String getVisibleText(TextView textView) {
// test that we have a textview and it has text
if (textView==null || TextUtils.isEmpty(textView.getText())) return null;
Layout l = textView.getLayout();
if (l!=null) {
// find the last visible position
int end = l.getLineEnd(textView.getMaxLines()-1);
// get only the text after that position
return textView.getText().toString().substring(0,end);
}
return null;
}
Ricorda: questo funziona dopo la vista è già caricato.
Usage:
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Log.i("test" ,"VisibleText="+getVisibleText(textView));
}
});
fonte
2017-11-25 11:22:38
è un'approssimazione va bene? – Snowball
Ciao. Sto lavorando allo stesso problema. Avete qualche soluzione per questo. Devo scoprire la parte visibile del testo da TextView. Mi potete aiutare? –
Per quanto ne so, non penso che ci sia un modo per farlo con TextView. – kosa