How to force an entire layout View refresh?

AndroidLayoutRefresh

Android Problem Overview


I want to force the main layout resource view to redraw / refresh, in say the Activity.onResume() method. How can I do this ?

By main layout view, I mean the one ('R.layout.mainscreen' below) that is called in my Activity.onCreate(), like this:-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainscreen);
}

Android Solutions


Solution 1 - Android

To strictly answer the question: Use invalidate():

> public void invalidate () Since: API Level 1

> Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();

Now, when the Activity resumes, it makes every View to draw itself. No call to invalidate() should be needed. To apply the theme, make sure you do it before any View is drawn, i.e., before setContentView(R.layout.mainscreen);

> public void setTheme (int resid) Since: API Level 1

> Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

The API doc reference is here: http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

Since the onDraw() method works on already instantiated Views, setTheme will not work. I have no experience with themes myself, but two alternative options I can think are:

  1. call setTheme in onCreate() instead, or
  2. redo setContentView (R.layout.mainscreen); to force reinstantiate all the layout.

Solution 2 - Android

Try getWindow().getDecorView().findViewById(android.R.id.content).invalidate();

Solution 3 - Android

Try recreate() it will cause this Activity to be recreated.

Solution 4 - Android

Solution:

Guys I tried all of your Solutions but they did not worked for me, I have to set setVisibility of EditText to VISIBLE and this EditText should be visible then in ScrollView, but I was unable to refresh root view to take effect. I solved my problem, when I need to refresh the view so I changed the ScrollView visibility to GONE and then again set it to VISIBLE to take effect and it worked for me. This is not the exact solution but it only worked.

 private void refreshView(){
    mScrollView.setVisibility(View.GONE);
    mScrollView.setVisibility(View.VISIBLE);
}

Solution 5 - Android

Calling invalidate() or postInvalidate() on the root layout apparently does NOT guarantee that children views will be redrawn. In my specific case, my root layout was a TableLayout and had several children of class TableRow and TextView. Calling postInvalidate(), or requestLayout() or even forceLayout() on the root TableLayout object did not cause any TextViews in the layout to be redrawn.

So, what I ended up doing was recursively parsing the layout looking for those TextViews and then calling postInvalidate() on each of those TextView objects.

The code can be found on GitHub: https://github.com/jkincali/Android-LinearLayout-Parser

Solution 6 - Android

Otherwise you can try this also-

ViewGroup vg = (ViewGroup) findViewById (R.id.videoTitleTxtView);
 vg.removeAllViews();
 vg.refreshDrawableState();

Solution 7 - Android

Just set your content view in onresume

setContentView(R.layout.yourview) inside onResume..

Example:

@Override
public void onResume(){
super.onResume();
setContentView(R.layout.activity_main); 
postIncomeTotals();
}

Solution 8 - Android

Try this workaround for the theming problem:

@Override
public void onBackPressed() {
	NavUtils.navigateUpTo(this, new Intent(this,
			MyNeedToBeRefreshed.class));
}

Solution 9 - Android

Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);

This allows you to reload with theme changes and hides the animations.

Solution 10 - Android

Try this

view.requestLayout();

Solution 11 - Android

I was having this problem as well but following Farhan's lead of using setContentView() I did just that. using setContentView() by itself was not enough however. I found that I had to repopulate all of my views with information. To fix this I just pulled all of that code out to another method (I called it build) and in my onCreate method I just call that build. Now whenever my event occurs that I want my activity to "refresh" I just call build, give it the new information (which I pass in as parameters to build) and I have a refreshed activity.

Solution 12 - Android

This appears to be a known bug.

Solution 13 - Android

I don't know if this is safe or not but it worked for me. I kinda ran into this problem myself and tried invalidate() and requestLayout() but to no avail. So I just called my onCreate(null) all over again and it worked. If this is unsafe please do let me know. Hope this helps someone out there.

Solution 14 - Android

This is how i used to Refresh my layout

Intent intent = getIntent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);

Solution 15 - Android

If you are going to create a custom view, make sure it is extending SurfaceView then, you can redraw it with method getHolder().lockCanvas(). Here is an example:

Canvas c = getHolder().lockCanvas();
c.drawText("Text", x, y, paint);
getHolder().unlockCanvasAndPost(c);

Solution 16 - Android

Not sure if it's good approach but I just call this each time:

setContentView(R.layout.mainscreen);

Solution 17 - Android

To clear a view extending ViewGroup, you just need to use the method removeAllViews()

Just like this (if you have a ViewGroup called myElement) :

myElement.removeAllViews()

Solution 18 - Android

Just call the activity again itself using the intent. For example to refresh the layout of MainActivity, do like this:

startActivity(new Intent(MainActivity.this, MainActivity.class));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMickeyRView Question on Stackoverflow
Solution 1 - AndroidAleadamView Answer on Stackoverflow
Solution 2 - AndroidkeyboardsurferView Answer on Stackoverflow
Solution 3 - AndroidM.KhouliView Answer on Stackoverflow
Solution 4 - AndroidNaveed AhmadView Answer on Stackoverflow
Solution 5 - AndroidjkincaliView Answer on Stackoverflow
Solution 6 - AndroidDatta KundeView Answer on Stackoverflow
Solution 7 - AndroidFarhan View Answer on Stackoverflow
Solution 8 - AndroidKrilivyeView Answer on Stackoverflow
Solution 9 - AndroidCodeversedView Answer on Stackoverflow
Solution 10 - AndroidMilind ChaudharyView Answer on Stackoverflow
Solution 11 - Androiduser3612162View Answer on Stackoverflow
Solution 12 - AndroidBen WilliamsView Answer on Stackoverflow
Solution 13 - AndroidCaiView Answer on Stackoverflow
Solution 14 - AndroidsivaBE35View Answer on Stackoverflow
Solution 15 - AndroidmindrexView Answer on Stackoverflow
Solution 16 - AndroidKrzysztof TkaczView Answer on Stackoverflow
Solution 17 - AndroidJack'View Answer on Stackoverflow
Solution 18 - AndroidAnshul AggarwalView Answer on Stackoverflow