Layout orientation in code

Greg picture Greg · Jun 7, 2011 · Viewed 92.8k times · Source

I have this code in my application:

LinearLayout.LayoutParams params =
    new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);

and I just want to set the orientation of the LinearLayout to vertical. The equivalent in XML is:

android:orientation="vertical"

How can I do it in the code, without XML?

Answer

Michael picture Michael · Jun 7, 2011

You can't change LinearLayout's orientation using its LayoutParams. It can be done only with a LinearLayout object.

LinearLayout layout = /* ... */;
layout.setOrientation(LinearLayout.VERTICAL);