4

What event signifies that the draw of a View is complete?

I know about ViewTreeObserver listeners, but I couldn't find the 'final' one, which indicates, that the job is done.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Bord81
  • 525
  • 2
  • 8
  • 23
  • what exactly do you want to achieve ? – Aman Grover Jul 13 '17 at 12:15
  • unclear what you are asking. – Andy Developer Jul 13 '17 at 12:18
  • 1
    I think it's pretty clear - `What event signifies that the draw of a TextView is complete?` – Dayan Jul 13 '17 at 12:19
  • 1
    I have a method which justifies text in a textview, so basically I call setText() first and then call this method. The issue is that the justification method starts too fast and gets inconsistent data from TextView with methods like getLineCount(). If I set a delay in 500 ms after setText() and before calling justify method everything works fine. But I would like to set up a listener instead which would signal me that I can laund justification and not set a manual delay. – Bord81 Jul 13 '17 at 12:22
  • can you post some code? – Giacomo Lai Jul 13 '17 at 12:31

3 Answers3

8

yourView.post(someRunnable) ensures, that someRunnable will be executed after the view is laid out and drawn.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • This is incorrect. All that View.post(Runnable) does is to post a message to the main thread. It does not wait for the view to laid out and drawn. – Enselic Dec 11 '20 at 11:54
  • @Enselic, you are correct. But there cannot be scenario, when `post(Runnable)` will be executed before view has been laid out, because the view layout is also `Message` itself that is posted on `MessageQueue`. The runnable is posted **after** this message, hence it's not theoretically possible that the runnable will get executed **before** the view is laid out. – azizbekian Dec 11 '20 at 13:28
  • Maybe, but the answer is still very misleading. If you `View.post(Runnable)` right after inflating it, the `Runnable` will run before `View.onDraw()` is even called. I just tested it. `View.post(Runnable)` will SOMETIMES work, but it is definitely not a reliable method. – Enselic Dec 11 '20 at 16:42
  • First thing I've found so far that works without having to override `onDraw()`. I just have to nest successive calls to `post()`, which makes my code a little ugly. But what can I say--it works. – SMBiggs Jan 31 '23 at 00:00
4

What event signifies that the draw of a TextView is complete?

There is no such hook for View class (or TextView). There is, however, onDraw() method which is called when the view should render its content.

So you can do:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // Finished drawing. Do other stuff.
    // However you must check if this is the first or subsequent call.
    // Each call to "invalidate()" will trigger re-drawing.
}
Siegmeyer
  • 4,312
  • 6
  • 26
  • 43
  • 1
    When the device supports hardware acceleration, View rendering will be done in the asynchronous thread RenderThread, Is onDraw can correctly get the rendering is complete? – kylin17 Feb 24 '18 at 03:10
2

If I understand your question correctly the method you are looking for is onWindowFocusChanged(boolean hasFocus). Or else you could try the onPostResume() method.

antonis_st
  • 468
  • 3
  • 16