Reputation: 1907
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);
}
Upvotes: 197
Views: 462234
Reputation: 166
parent.invalidate() won't work without parent.requestLayout(). so, use
parent.invalidate()
container.requestLayout()
Upvotes: 1
Reputation: 269
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);
Upvotes: 0
Reputation: 97
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));
Upvotes: -13
Reputation: 39
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.
Upvotes: 3
Reputation: 488
Not sure if it's good approach but I just call this each time:
setContentView(R.layout.mainscreen);
Upvotes: -1
Reputation: 1891
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);
Upvotes: 0
Reputation: 9473
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.
Upvotes: 4
Reputation: 6747
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);
}
Upvotes: 38
Reputation: 5333
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.
Upvotes: 0
Reputation: 1025
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
Upvotes: 12
Reputation: 557
Try this workaround for the theming problem:
@Override
public void onBackPressed() {
NavUtils.navigateUpTo(this, new Intent(this,
MyNeedToBeRefreshed.class));
}
Upvotes: 4
Reputation: 3226
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();
}
Upvotes: 5
Reputation: 40401
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:
Upvotes: 160
Reputation: 467
Otherwise you can try this also-
ViewGroup vg = (ViewGroup) findViewById (R.id.videoTitleTxtView);
vg.removeAllViews();
vg.refreshDrawableState();
Upvotes: 9
Reputation: 17970
Try getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
Upvotes: 43