private void scaleAllViews(ViewGroup parentLayout) {
int count = parentLayout.getChildCount();
Log.d(TAG, "scaleAllViews: "+count);
View v = null;
for (int i = 0; i < count; i++) {
try {
v = parentLayout.getChildAt(i);
if(((ViewGroup)v).getChildCount()>0){
scaleAllViews((ViewGroup)v);
}else{
if (v != null) {
v.setScaleY(0.9f);
}
}
} catch (NullPointerException e) {
}
}
}
I created a recursive function to access child items of view group, but the parentLayout.getChildAt(i);
returns a View
, which contains children too, so I need to access that, but after casting I get the error java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.view.ViewGroup
You need to check if it is a ViewGroup
before casting to ViewGroup
.
if(v instanceof ViewGroup) {
// now this is a safe cast
ViewGroup vg = (ViewGroup) vg;
// ... use this ViewGroup
} else {
// It's some other type of View
}