How to force an entire layout View refresh?
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 o开发者_StackOverflownCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
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:
- call setTheme in onCreate() instead, or
- redo setContentView (R.layout.mainscreen); to force reinstantiate all the layout.
Try getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
Try recreate()
it will cause this Activity to be recreated.
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);
}
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
Otherwise you can try this also-
ViewGroup vg = (ViewGroup) findViewById (R.id.videoTitleTxtView);
vg.removeAllViews();
vg.refreshDrawableState();
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();
}
Try this
view.requestLayout();
Try this workaround for the theming problem:
@Override
public void onBackPressed() {
NavUtils.navigateUpTo(this, new Intent(this,
MyNeedToBeRefreshed.class));
}
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.
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.
This appears to be a known bug.
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.
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);
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);
parent.invalidate() won't work without parent.requestLayout(). so, use
parent.invalidate()
container.requestLayout()
Not sure if it's good approach but I just call this each time:
setContentView(R.layout.mainscreen);
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()
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));
精彩评论