2

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?

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
purewatashi21
  • 151
  • 3
  • 12
  • It seems that one of your layouts is a `RelativeLayout` – Juan Cruz Soler Jul 12 '16 at 00:21
  • The type of `LayoutParams` a `View` has depends on its parent `View`, not what it itself is. Your `LinearLayout` appears to be in a `RelativeLayout`, and so has `RelativeLayout.LayoutParams`, not `LinearLayout.LayoutParams`. – Mike M. Jul 12 '16 at 00:22
  • @JuanCruzSoler The layouts I am modifying are Linear. The relative layout is pretty much the parent of all the objects, but the objects I want to manipulate are Linear – purewatashi21 Jul 12 '16 at 01:31

1 Answers1

5

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

afhamu
  • 930
  • 11
  • 17
Franklyn
  • 325
  • 2
  • 8
  • Ah okay. Would there be any way of setting an "alignParentBottom" value to false? – purewatashi21 Jul 12 '16 at 12:30
  • @purewatashi21 You need to make sure the `LayoutParams` are `RelativeLayout.LayoutParams`, then you can use: `params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);`. – Mike M. Jul 12 '16 at 16:01
  • @purewatashi21, exactly what Mike M. said, you have to use a RelativeLayout before you can use the attribute alignParentBottom. – Franklyn Jul 13 '16 at 00:44