How to set Width and Height of FrameLayout dynamically android?

mad_greasemonkey picture mad_greasemonkey · Oct 14, 2016 · Viewed 17.5k times · Source

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

Answer

sushildlh picture sushildlh · Oct 14, 2016

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....