Search code examples
javaandroidandroid-layoutandroid-linearlayout

Adjusting height of linear layout programmatically


Whenever I try setting the height of a LinearLayout, I always get this exception:

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

Here is my code:

LinearLayout.LayoutParams  hide = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
LinearLayout.LayoutParams  show = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);

driverMainOptions.setLayoutParams(hide);
mapDirections.setLayoutParams(show);

Is there a specific import statement I need for this to execute properly?


Solution

  • This should work

    LinearLayout lLayout = new LineaLayout(context);
    LayoutParams params = lLayout.getLayoutParams();
    
    params.height = 200;
    params.width = 200;
    lLayout.setLayoutParams(params);
    

    see the accepted answer at Android: How to Programmatically set the size of a Layout