Take the 2-minute tour ×

How can I do getLineCount() of an Edittext in the onCreate() method of an activity, having changed the Edittext's text, like the following:

    public void onCreate(Bundle savedInstanceState){

Because the view has not been drawn yet getLineCount() will always return 0. Is there a way to get around this problem? Thanks!

share|improve this question
add comment

3 Answers

Ugh, it's a problem with UI everywhere.

You can use a Handler. You'll post a Runnable that will get the line count and continue the processing.

share|improve this answer
I can see what you are saying and it should work. But I can't get it to! Is this what you mean: new Handler().post(new Runnable(){public void run() { myEditText.getLineCount() } }); It still returns 0! –  Henry Thompson Nov 10 '11 at 23:19
That is indeed frustrating. Try hooking into a later event - such as onStart or even onResume. If you can get the line count there, great, if not - post the message there. –  zmbq Nov 11 '11 at 20:06
I've tried those. They both return 0 too! I could count the number of "\n" in the getText() (which works even in onCreate()) but it seems too inefficient especially if its loading up a 50,000 character String! The reason I actually need it is because I'm trying to implement line numbering by putting a textview with the same size text left of the edittext. Are there any other ways I could create a line numbering system? Thanks a lot for your help! –  Henry Thompson Nov 11 '11 at 20:26
Oh, I wouldn't do that. To add line numbers, I would use a TableLayout, with the left column for line numbers and the right column for the text itself. One TextView per line number and one TextView per line. –  zmbq Nov 11 '11 at 22:05
Why would you do it one TextView per line? How would the user edit the text? And how would you do line numbering with one textview per line number? Surely the fact there will be potentially ten thousand or whatever TextViews for each line number is far more expensive? –  Henry Thompson Nov 11 '11 at 22:10
show 5 more comments

This is definitely a pain. In my case I didn't need editing, so I worked with TextView but seeing as EditText derives from TextView you should be able to use the same approach. I subclassed TextView and implemented onSizeChanged to call a new listener which I called OnSizeChangedListener. In the listener you can call getLineCount() with valid results.

The TextView:

/** Size change listening TextView. */
public class SizeChangeNotifyingTextView extends TextView {
    /** Listener. */
    private OnSizeChangeListener m_listener;

     * Creates a new Layout-notifying TextView.
     * @param context   Context.
     * @param attrs     Attributes.
    public SizeChangeNotifyingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

     * Adds a size change listener.
     * @param listener  Listener.
    public void setOnSizeChangedListener(OnSizeChangeListener listener) {
        m_listener = listener;

        super.onSizeChanged(w, h, oldw, oldh);
        if (m_listener != null) {
            m_listener.onSizeChanged(w, h, oldw, oldh);
share|improve this answer
Ah that should work! I'll give it a go later on. Thanks for the response. –  Henry Thompson Nov 17 '11 at 22:36
add comment

Thanks a lot for the last answer, it was very helpful to me!

As a contribution, I would like to add the code of the listener interface that will be registered to the SizeChangeNotifyingTextView's hook (which can be added inside the SizeChangeNotifyingTextView class):

public interface OnSizeChangeListener {
    public void onSizeChanged(int w, int h, int oldw, int oldh);

Finally, to register a listener, you can do it this way:

tv.setOnSizeChangedListener(new SizeChangeNotifyingTextView.OnSizeChangeListener() {
share|improve this answer
add comment

Your Answer


