Android Edittext: Get LineCount in an Activity's onCreate()
Asked Answered
C

3

3

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

    @override
    public void onCreate(Bundle savedInstanceState){
        myEditText.setText("EXAMPLE");
        myEditText.getLineCount();
    }

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

Confounded answered 10/11, 2011 at 21:9 Comment(0)
I
2

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.

Indentation answered 10/11, 2011 at 21:17 Comment(10)
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!Confounded
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.Indentation
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!Confounded
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.Indentation
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?Confounded
Also how does an app like Jota do their line numbering? I know its open source so I might look at the code.Confounded
Oh, good point, you want the user to type text there, silly me... You obviously don't need 10000 TextViews, just one per line on the phone screen, which is finite. Although I agree it makes little sense now. How does Jota do it?Indentation
Jota have taken Google's textview and modified it so their line numbering is actually part of the textview. Its a bit too complicated for me... I think I will stick with the second textview for line numbering though.Confounded
In that case, why don't you just fill the line-number textview with as many numbers as you want, but resize it to be the size of the first textview?Indentation
Could do. I will have a think about it. Thanks very much you have been a great help!Confounded
F
2

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;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (m_listener != null) {
            m_listener.onSizeChanged(w, h, oldw, oldh);
        }
    }
}
Fathom answered 14/11, 2011 at 23:8 Comment(1)
Ah that should work! I'll give it a go later on. Thanks for the response.Confounded
R
1

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() {
     @Override
     public void onSizeChanged(int w, int h, int oldw, int oldh) {
       ...
     }
});
Raglan answered 24/2, 2012 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.