Search code examples
javaandroidandroid-viewviewgroup

Android : How to get view class from a view to use with instanceOf


I'm trying to get a view by type inside a viewgroup by returning the first view found view.

private View findViewByType(ViewGroup p, View v) {
    for (int i = 0; i < p.getChildCount(); i++)
        if (p.getChildAt(i) instanceof v.getClass()) 
            return p.getChildAt(i);
}

I can't seem to get view.getClass() to work. How do I get the class from a view?


Solution

  • One way could be

    p.getChildAt(i).getClass().isInstance(v)
    

    isIsntance(Object) determines if the specified Object is assignment-compatible with the object represented by this Class. You can read the documentation here