Jaume
Jaume

Reputation: 3780

android redraw layout programmatically

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

Upvotes: 6

Views: 37801

Answers (4)

Cai
Cai

Reputation: 5323

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.

Upvotes: -1

pawelzieba
pawelzieba

Reputation: 16082

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

Upvotes: 1

AZ13
AZ13

Reputation: 14517

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.

Upvotes: 2

Floern
Floern

Reputation: 33904

Did you try to invalidate() the root view?

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

This should redraw the whole layout.

Upvotes: 8

Related Questions