Try using LayoutParams:
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.root_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
ImageView btnSend = new ImageView (this);
btnSend.setLayoutParams(params);
rootLayout.addView(btnSend);
The only problem is that I donβt remember if it params.gravitysets the gravity of the content or layout_gravity. Layout_gravity is what you want to change in this instance.
source
share