Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout orientation in code

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?

like image 788
Greg Avatar asked Jun 07 '11 06:06

Greg


People also ask

What is orientation in linear layout?

The orientation attribute is used to arrange its children either in horizontal or vertical order. The valid values for this attribute are horizontal and vertical. If the value of the android:orientation attribute is set to vertical, the children in the linear layout are arranged in a column layout, one below the other.

How do you change linear layout to vertical?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

What is a linear layout?

Android LinearLayout is a view group that aligns all children in either vertically or horizontally.

What is layout_weight and weightSum in android?

Answer: Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly. Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it.


2 Answers

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); 
like image 174
Michael Avatar answered Sep 26 '22 14:09

Michael


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); 
like image 44
Balaji Khadake Avatar answered Sep 23 '22 14:09

Balaji Khadake