I have a TextView that getLayout returns null. Which is “normal” according to the documentation, because before, I added it to the layout and then changed its width.
how to avoid this?
(I tried using OnGlobalLayoutListener and then expected that it has full width, but it does not always work)
EDIT: I call from the list adapter in getView ():
drawArticle(article, true);
Here is the drawArticle () method:
TextView txt; Spanned text; private void drawArticle(final Article article, boolean first) { if(first){ text = Html.fromHtml(article.content); } else { text = Html.fromHtml(getInvisibleText(txt)); } txt = createTextView(); txt.setText(text); layout.addView(txt); vto = txt.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { txt.getViewTreeObserver().removeGlobalOnLayoutListener(this); String inVisibleText = getInvisibleText(txt); if (inVisibleText != null && !inVisibleText.isEmpty()) { drawArticle(article, false); } } }); }
and
private String getInvisibleText(final TextView textView) { Layout staticLayout = null; String toDislay = ""; int height = textView.getHeight(); int scrollY = textView.getScrollY(); staticLayout = textView.getLayout(); int lastVisibleLineNumber = staticLayout.getLineForVertical(scrollY+height); int start = staticLayout.getLineEnd(lastVisibleLineNumber); int end = staticLayout.getLineEnd(textView.getLineCount()-1); toDislay = textView.getText().toString().substring(start, end); return toDislay; }
Suppose to set a very long text to a text view, then get what is not displayed, in order to recursively put other text images
source share