Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set both gravity and layout gravity of a LinearLayout programatically

I am risking writing a duplicate question, but as I was researching the answer to another SO question, I realized that I couldn't find a simple question and answer that included setting both gravity and layout_gravity of a LinearLayout. Also, I was confused as to the difference between them when talking about a ViewGroup rather than just a view. I am answering my question below.

Here are some of the other questions I viewed:

  • How to set gravity to layout programmatically?
  • Set gravity for LinearLayout programmatically
  • android - setting LayoutParams programmatically
  • LayoutParams gravity not working
  • layout_gravity in LinearLayout
  • Problem with setLayoutParams method in android UI
  • how to set setLayoutParams for linear layout elements
  • Is it possible to change the layout width and height in Android at run time?
like image 996
Suragch Avatar asked Nov 23 '14 07:11

Suragch


People also ask

How do you set gravity in linear Layout?

Remember that gravity sets the positioning of a view's children, while layout_gravity sets the positioning of a view within its parent. So in your case, you want to set the gravity of the LinearLayout, which can be done via member methods. You should also set the orientation. Show activity on this post.

How to make LinearLayout 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 orientation property in LinearLayout?

The LinearLayout is the most basic layout, and it arranges its elements sequentially, either horizontally or vertically. To arrange controls within a linear layout, the following attributes are used: android:orientation—Used for arranging the controls in the container in horizontal or vertical order.

Which attribute set the gravity of the view or Layout in parent?

android:layout_gravity sets the gravity of the View or Layout relative to its parent.


2 Answers

Short answer

Set gravity

linearLayout.setGravity(Gravity.CENTER);

Set layout gravity

// the LinearLayout's parent is a FrameLayout      
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
linearLayout.setLayoutParams(params);

Background

Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.

Setting the gravity of a LinearLayout itself changes the location of the views within it. Setting the layout_gravity of a LinearLayout changes how the LinearLayout is arranged within its parent layout.

This image shows a LinearLayout (brown) within a FrameLayout (white). The LinearLayout's gravity is set to center_horizontal and its layout_gravity is set to right|bottom.

enter image description here

Here is the xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/llExample"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="right|bottom"
        android:background="#e3e2ad"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#bcf5b1"
            android:text="TextView 1" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#aacaff"
            android:text="TextView 2" />

    </LinearLayout>

</FrameLayout>

Changing things programmatically

The following code shows how to change both the gravity and the layout_gravity of the LinearLayout.

public class LinearLayoutGravity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear_layout_gravity);

        // Change the gravity (not layout_gravity) of the LinearLayout
        LinearLayout ll = (LinearLayout) findViewById(R.id.llExample);
        ll.setGravity(Gravity.CENTER);

        // Change the layout_gravity (not gravity) of the LinearLayout      
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
        params.gravity = Gravity.TOP|Gravity.RIGHT;
        ll.setLayoutParams(params);
    }
}

And here is the result:

enter image description here

See also

  • RelativeLayout and LinearLayout with gravity: What is the effect?
like image 111
Suragch Avatar answered Oct 27 '22 18:10

Suragch


If you are using KOTLIN use or operator like this:

GRAVITY = Gravity.RIGHT or Gravity.CENTER_VERTICAL

frameContainer.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                     FrameLayout.LayoutParams.WRAP_CONTENT,
                     GRAVITY)
like image 40
Hitesh Sahu Avatar answered Oct 27 '22 16:10

Hitesh Sahu