force layout to refresh/repaint android?

30,295

Solution 1

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

Solution 2

After several hours of testing, I found the solution about updating a view if you made operation with these views like adding children, visibility, rotation, etc.

We need to force update the view with the below methods.

linearSliderDots.post {
        // here linearSliderDots is a linear layout &
        // I made add & remove view option on runtime
        linearSliderDots.invalidate()
        linearSliderDots.requestLayout()
    }
Share:
30,295
Youssef Maouche
Author by

Youssef Maouche

Updated on September 17, 2020

Comments

  • Youssef Maouche
    Youssef Maouche almost 4 years

    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() .

  • prc
    prc almost 11 years
    Where is this code placed? If you are running this code on the UI thread, then you will have the app stopped and possibly nothing as result. Please be more specific with your questions and also don't change the question without marking it with "EDIT:"
  • Youssef Maouche
    Youssef Maouche almost 11 years
    thanks for your quick response, and i'am sorry because i new here, and about code is work but don't show the result, all layout is up in the same time after 750ms , and i don't use thread thnks.
  • prc
    prc almost 11 years
    Then you need to have this running in a separate thread and use runOnUIThread() whenever you need to update the UI.
  • Alex.F
    Alex.F almost 6 years
    I don't understand how this answers the question. Could you explain how this answers the question?