97

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?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
grgvn
  • 1,309
  • 1
  • 12
  • 25

6 Answers6

203

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);
Michael
  • 53,859
  • 22
  • 133
  • 139
12

You can use like this one:

LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
myll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
myll.setOrientation(LinearLayout.VERTICAL);
Hardik
  • 17,179
  • 2
  • 35
  • 40
Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38
  • 4
    the second line should be `myLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);` – otaku Feb 13 '15 at 07:31
4

You need to instance LinearLayout. After that you can call setOrientation()

LinearLayout myLayout = ...;
myLayout.setLayoutParams(new LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myLayout.setOrientation(LinearLayout.VERTICAL);

That should do the job :)

For more infos check the Android API.

dudeldidadum
  • 1,281
  • 17
  • 27
2

A working sample below (it's LayoutParams.WRAP_CONTENT, NOT LinearLayout.WRAP_CONTENT)

myLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.setLayoutParams(layoutParams);
Tia
  • 95
  • 2
  • 8
2

In case anyone else arrives here like me looking for the answer for Xamarin, the equivalent is:

LinearLayout layout = /* ... */;
layout.Orientation = Orientation.Vertical;
layout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
stovroz
  • 6,835
  • 2
  • 48
  • 59
-6

Simply use as follow :-

LinearLayout mlayout = new LinearLayout(context);
mlayout.setOrientation(2);

2 means Vertical, 1 is used for horizontal.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
  • 2
    Why would you come back 4 years after the answer was accepted and give a worse answer? You should not use '2'. You should use the static values defined in LinearLayout, e.g. LinearLayout.Vertical. – Stealth Rabbi Mar 04 '16 at 15:18
  • Because I wanted to know If I am right or wrong. :) Now I learnt that it is better to use static values defined in linearLayout. :-) – Yama Raahi Mar 06 '16 at 10:04
  • 1
    Then why not just look at the correct answer? Stack Overflow should not be used to put your guesses as ANSWERS. If you don't know how to answer the question correctly, then don't write an answer. Now when someone comes to this page, they have to know to ignore your wrong answer and looking for better ones. This is why you're being downvoted. – Stealth Rabbi Mar 07 '16 at 12:45
  • I just wanted to contribute my friend, in my case this one worked and I commented for him. BTW, if it is wrong to use 2 and 1, then why the hell it exist.? – Yama Raahi Mar 08 '16 at 14:52
  • It exists because it is the raw value attached to the static constants defined in LinearLayout as @StealthRabbi pointed out. However, you should never ever use those values. – Daniel Molina Sep 30 '16 at 09:39