Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use textview.getLayout()? It returns null

I’m trying set a layout for textview so I can use getEllipsisCount() method. But below code returns null as layout value. How can I take layout and then use getEllipsisCount(0) method.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView mytextview =(TextView) findViewById(R.id.textView1);
        mytextview.setText(myText);
    
        Layout layout = mytextview.getLayout();
        if(layout != null){
            mytextview.setText("very good layout worked\n");
        }
    }
}
like image 639
Soroush Aghaiee Avatar asked May 15 '13 07:05

Soroush Aghaiee


4 Answers

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();  
        }
    });
like image 169
Pragnani Avatar answered Nov 18 '22 06:11

Pragnani


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.

like image 38
gipi Avatar answered Nov 18 '22 04:11

gipi


Call

TextView.onPreDraw();

It creates the layout.

like image 5
Wonsik Sung Avatar answered Nov 18 '22 06:11

Wonsik Sung


I recently encountered this problem myself. For any newcomers to this thread, the accepted answer by @Pragnani along with the comment from @dimsuz provides a nice and compact solution. Adding an OnPreDrawListener to the view tree of the text view, wait for a reference to a layout and use it, and lastly remove the listener after any work requiring the layout is finished.

Given the example code from OP, implementing the solution could look something like this:

public class MainActivity extends Activity implements ViewTreeObserver.OnPreDrawListener {

    // maintain a reference to the view so we can later remove the listener
    private TextView mytextview;

    @Override
    public boolean onPreDraw() {
        Layout layout = mytextview.getLayout();
        if (layout != null) {

            // use layout here...
            int n = layout.getEllipsisCount(0);

            // work requiring a layout is finished; remove listener from view
            mytextview.getViewTreeObserver().removeOnPreDrawListener(this);
        }
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mytextview = (TextView) findViewById(R.id.textView1);

        // add listener to the view
        mytextView.getViewTreeObserver().addOnPreDrawListener(this);
    }
}
like image 1
Adam Martinu Avatar answered Nov 18 '22 04:11

Adam Martinu