Search code examples
androidlayoutrefresh

force layout to refresh/repaint android?


I want to change position of layout and after 75ms return it to first position to make a movement and that is my code:

for(int i = 0; i < l1.getChildCount(); i++) {  
    linear = (LinearLayout) findViewById(l1.getChildAt(i).getId());  
    LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
    params.bottomMargin = 10;  
    linear.setLayoutParams(params);  
    SystemClock.sleep(75);
}   

The problem is the app is stop for 750ms and don't do anything. I tried invalidate() , refreshDrawableState(), requestLayout(), postInvalidate(), and try to call onResume(), onRestart(), onPause() .


Solution

  • Maybe you need:

    linear.invalidate();
    linear.requestLayout();
    

    after making the layout changes.

    EDIT:

    Run the code on a different thread:

    new Thread() {
        @Override
        public void run() {
            <your code here>
        }
    }.start();
    

    And whenever you need to update the UI from that thread use:

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            <code to change UI>
        }
    });