Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 370
Jaume Avatar asked Jul 05 '12 15:07

Jaume


2 Answers

Did you try to invalidate() the root view?

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

This should redraw the whole layout.

like image 57
Floern Avatar answered Oct 06 '22 11:10

Floern


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.

like image 34
AZ13 Avatar answered Oct 06 '22 12:10

AZ13