6

My layout sometimes does not position elements properly. I tried a lot of solutions but unique working one is rotate from portrait to landscape and then return from landscape to portrait. Then layout is automaticaly redrawed and fits perfectly. I set orientation|keyboard so content is not reloaded, only redrawed. How to redraw content programmaticaly like landscape-portrait does? Thank you.

EDIT: I tried following and has no effect

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

Activity has set main layout on onCreate setContentView(R.layout.main);

Layout "main" contains a TabHost and Admob. Layout "view" contains a webview ("id:myWebview") that is shown on tab1.

And also tried to invalidate them! webview, layout main and layout view and crashes

Jaume
  • 3,318
  • 16
  • 50
  • 108
  • What does "sometimes" means? Is there any reproducible way to get the error? Have you tried requestLayout to force to redraw the layout? Can you put some sample code to show what kind of and views are you using? – Luis Jul 05 '12 at 15:56
  • You could try calling `invalidate()` to force a view to redraw. – eternalmatt Jul 05 '12 at 15:57

4 Answers4

6

Did you try to invalidate() the root view?

findViewById(android.R.id.content).invalidate();

This should redraw the whole layout.

Community
  • 1
  • 1
Floern
  • 31,495
  • 23
  • 98
  • 115
2

When screen orientation changes, Android recreates the current Activity.

Try calling measure(int, int) or requestLayout(). Otherwise there is no chance to programmatically relayout views.

AZ13
  • 13,877
  • 5
  • 32
  • 31
1

Call requestLayout() only and read View class description for more informations.

pawelzieba
  • 15,976
  • 3
  • 42
  • 72
-1

If this is unsafe or bad practice please do correct me. I tried all these methods as well and out of desperation decided to just try calling my onCreate again and it worked. So for now I'll be using onCreate(null) to refresh my view when I update data. Again, please let me know if this is unsafe or a bad practice and if possible a working alternative.

Cai
  • 3,807
  • 3
  • 15
  • 23