I'm getting exception when i do following:
findViewById(R.id.some_container).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50));
But when I change to new LinearLayout.LayoutParams
that's ok. If LinearLayout.LayoutParams
is child of ViewGroup.LayoutParams
that why I can't cast?
All LinearLayout
is also a ViewGroup
but not all ViewGroup
is a LinearLayout
. That's why you can't cast a ViewGroup
to a LinearLayout
You can create a LinearLayout.LayoutParams
from a ViewGroup.LayoutParams
as follows:
LinearLayout.LayoutParams params =
new LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50));
However, a simple cast is impossible.