Skip to content Skip to sidebar Skip to footer

Align Imageview In Linearlayout By Code

the question is simple, i am creating a imageview dynamically using code. ImageView btnSend = new ImageView (this); and add it to a LinearLayout, the problem is that I want to lea

Solution 1:

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 params.gravity sets the content gravity or layout_gravity. Layout_gravity is what you're wanting to change in this instance.

Solution 2:

You have to call setGravity() on LinearLayout:

yourLinLay.setGravity(Gravity.RIGHT);

or in XML:

<LinearLayoutandroid:gravity="right">

http://developer.android.com/reference/android/widget/LinearLayout.html#setGravity%28int%29 http://developer.android.com/reference/android/view/Gravity.html

Post a Comment for "Align Imageview In Linearlayout By Code"