Android – How to set Width and Height of FrameLayout dynamically android

androidandroid-fragmentsandroid-framelayoutandroid-linearlayoutclasscastexception

I am trying to set the width and height of 2 frame layouts which are the containers for 2 fragments, both being children of a Linear Layout.

However whenever I set width and height of the layout with

frameLayout.setLayoutParams(new FrameLayout.LayoutParams(100,100);

it is resulting in a class cast Exception

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

Best Answer

use this method ...

.setLayoutParams(new LinearLayout.LayoutParams(100,100);

instead of your method ....

.setLayoutParams(new FrameLayout.LayoutParams(100,100)

Note:- ClassCastException problem happen when your class is different other class, you want to cast....