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

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 … Read more