title: How to force a view to redraw immediately before the next line of code is executed
body: <p>I have a LinearLayout that I would like to change the background color of when one of its child views (an ImageButton) is clicked.  I am able to do this, but not immediately - the change doesn't occur on the screen until later (I think during an onResume call).  I would like to find out how to force the layout to redraw after the background is set, before the next line of code executes.  Here is the onClick method of my OnClickListener for the button:</p>
  
  <pre><code>public void onClick(View v) {
  
    LinearLayout parentLayout = (LinearLayout) v.getParent();
    parentLayout.setBackgroundResource(R.color.my_color);
    SystemClock.sleep(1000); //ms
  
  }
  </code></pre>
  
  <p>The sleep command is in there to test whether the redraw happens before or after it.  The result: after.  Most questions on this topic (like <a href="https://stackoverflow.com/questions/1017853/android-how-to-redraw-a-graphic-element">here</a> and <a href="https://stackoverflow.com/questions/5991968/how-to-force-an-entire-layout-view-refresh">here</a>) say to use invalidate() on the view.  I have used the commands <code>parentLayout.invalidate();</code>, <code>parentLayout.postInvalidate();</code>, and <code>parentLayout.refreshDrawableState();</code> in between the background and sleep lines, all to no avail.  The redraw still happens after the sleep command.  Can anyone tell me how to make it happen immediately?</p>
  
  <p>Other possibly useful information:  The LinearLayout is bound to the rows of a ListView, and the OnClickListener above is in a custom class that extends SimpleCursorAdapter, not in the activity itself.  (This way I can set a listener for each of the rows in the ListView.)</p>