Layout orientation in code
Asked Answered
C

6

98

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?

Castello answered 7/6, 2011 at 6:38 Comment(0)
L
204

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);
Lukelukens answered 7/6, 2011 at 6:40 Comment(0)
U
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);
Unexpressed answered 7/6, 2011 at 6:51 Comment(1)
the second line should be myLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);Bay
C
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.

Challis answered 7/6, 2011 at 6:45 Comment(0)
A
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);
Agata answered 9/3, 2014 at 18:49 Comment(0)
D
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);
Dichroism answered 11/7, 2017 at 11:58 Comment(0)
F
-7

Simply use as follow :-

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

2 means Vertical, 1 is used for horizontal.

Fauteuil answered 26/12, 2015 at 5:38 Comment(5)
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.Drumhead
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. :-)Fauteuil
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.Drumhead
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.?Fauteuil
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.Anxious

© 2022 - 2024 — McMap. All rights reserved.