suku suku - 26 days ago 11
Android Question

How to find the layout type of a view?

I want to avoid

ClassCastException
on doing
this.getLayoutParams()
. Is there a way by which I can get the layout type i.e Relative, Linear or Frame?
Then I would like to use a switch case on the result and then do
this.getLayoutParams()
.

Answer

You can use instanceof operator.

if(view instanceof RelativeLayout) {
    // do something
} else if(view instanceof LinearLayout) {
    // do something
} else if(view instanceof FrameLayout) {
    // do something
}