Android Edittext: Get Linecount In An Activity's Oncreate()
Solution 1:
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.
Solution 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. */publicclassSizeChangeNotifyingTextViewextendsTextView {
/** Listener. */private OnSizeChangeListener m_listener;
/**
* Creates a new Layout-notifying TextView.
* @param context Context.
* @param attrs Attributes.
*/publicSizeChangeNotifyingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Adds a size change listener.
* @param listener Listener.
*/publicvoidsetOnSizeChangedListener(OnSizeChangeListener listener) {
m_listener = listener;
}
@OverrideprotectedvoidonSizeChanged(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);
}
}
}
Solution 3:
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):
publicinterfaceOnSizeChangeListener {
publicvoidonSizeChanged(int w, int h, int oldw, int oldh);
}
Finally, to register a listener, you can do it this way:
tv.setOnSizeChangedListener(newSizeChangeNotifyingTextView.OnSizeChangeListener() {
@OverridepublicvoidonSizeChanged(int w, int h, int oldw, int oldh) {
...
}
});
Post a Comment for "Android Edittext: Get Linecount In An Activity's Oncreate()"