How to use textview.getLayout()? It returns null
From the documentation:
public final Layout getLayout ()
Added in API level 1 Returns the Layout that is currently being used to display the text. This can be null if the text or width has recently changes.
So probably your text has changes or you call it too early.
Look at this answer where is stated
This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.
You are calling it too early, thats why it is returning null
Try this
ViewTreeObserver vto = mytextview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout layout = mytextview.getLayout();
}
});
Call
TextView.onPreDraw();
It creates the layout.