Search code examples
android-2.1-eclair

How Android View Hierarchies work


I have a WebView that I would like to expand to fullscreen. The user will interact with it, so it needs to maintain some sort of state when it expands. The ideal process is this:

- Remove WebView from its parent
- Put WebView at the top of the current View hierarchy with `FILL_PARENT` for width and height

Later, I will need the put the WebView back:

- Remove WebView from top of the hierarchy
- Restore the old hierarchy
- Put WebView back where it was with its previous setting for width and height

I know about getRootView, but I don't know how to use it. What's a DecorView?

If anyone has some sample code to accomplish the expanding and collapsing behavior I've described above, I'd be really grateful. Thanks!


Solution

  • Get the root view of the DecorView:

    ViewGroup parentViewGroup = (ViewGroup) webView.getRootView().findViewById(android.R.id.content);
    View rootview = parentViewGroup.getChildAt(0);
    

    Remove that root view:

    parentViewGroup.removeViewAt(0);
    

    Remove the WebView from its parent:

    View webViewParent = (View) webView.getParent();
    webViewParent.removeView(webView);
    

    Put the WebView at the top of the view hierarchy:

    parentViewGroup.addView(webView, 0, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
    

    Use your rootview reference to undo the expansion.

    The only thing is: I don't know how you're going to restore the layout parameters for rootview.