tags:

views:

100

answers:

2

Hi,

I wonder if it is possible to get a decorViews child. For example if I have a decorView object and I know that the child of the decorView is a FrameLayout, is there someway for me to get the FrameLayout object just by using its parent?

Obviously I could use getViewById() in the traditional way, but if I only want to use the parent object, is it possible to get the FrameLayout object?

Thanks,

A: 

Are you talking about object inheritance?

If that is the case, then it is simple: Cast it.

Assuming you have a class "Foo" which is an ancestor-class of "Bar" (in other words: Bar is a "child" of Foo), then take this example:

Foo myFoo = getSomeFoo();
Bar myBar = (Bar)myFoo;

So, adapting this to your question:

View myView = getDecorView();
FrameLayout myFrameLayout = (FrameLayout) myView;

or:

FrameLayout myFrameLayout = (FrameLayout) getDecorView();

You should protect the block making this call though as this will throw a ClassCastException if the instance returned by getDecorView is not a FrameLayout (or a (in)direct subclass of it). Simply add a try/catch block, or add a throwing declaration to the method containing this line. Whatever suits you best.

exhuma
Thanks, It works!
Renas
A: 

That's not an answer. DecorView is a private class, so you can't cast to it.

edrowland