2016年6月3日 星期五

Detect if TextVIew is ellipsized

Code snippet
//Hide/Show More buttion
final TextView detail   = (TextView) findViewById(R.id.detail);
final TextView more     = (TextView) findViewById(R.id.more);
ViewTreeObserver vto = detail.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
                ViewTreeObserver observer = detail.getViewTreeObserver();
                observer.removeOnPreDrawListener(this);
                String text         = detail.getText().toString();
                String layoutText   = detail.getLayout().getText().toString();
                more.setVisibility((!text.equals(layoutText)) ? View.VISIBLE:View.INVISIBLE);
                return true;
        }
});

沒有留言: